#INTRO: LOADING PACKAGES ETC----
library(foreign)
library(reshape2)
library(data.table)
library(stargazer)
library(MatchIt)
#library(nonrandom)
library(ggplot2)
#library(gdata1)
library(gmodels)
library(gridExtra)
library(haven)
library(plyr)
#library(car)
#library(xlsx)
library(tidyr)
library(reshape)
library(countrycode)
#library(dGlyr)
library(readr)
library(gnm)
library(MNP)
library(nls2)
library(nlstools)
library(stats)
library(dplyr)
library(matrixStats)
library(miceadds)
library(minpack.lm)
library(lfe)
library(lubridate)
library(AER)
library(reshape)
library(reshape2)
library(tictoc)
library(xtable)
library(Formula)
library(mlogit)
library(plm)
library(survival)
library(plotly)
library(alpaca)
library(lmtest)
library(sandwich)
# library(mnlogit)

library(foreach)#parallel foreach
library(iterators)
library(parallel)
library(doParallel)

library(msm)
library(tikzDevice)

library(openxlsx)#read.xlsx

library(labelled)#change labels to columns

options(width=1000)#set max characters shown
options(max.print=100000)#set max rows shown console

# !diagnostics suppress=Dsvr,Dgdm

rm(list=ls())
cat("\014")


#READ DATA:----

##read CSES data:
load("d29_CSES.RData")
dc=d
rm(d)


##read Leadership Project data (LP); Political Leaders and Democratic Elections:
dl=read_dta("LEADERS.dta")

#code variables:
colnames(dl)[colnames(dl)=="COUNTRY"]="Ec"
colnames(dl)[colnames(dl)=="ELECTION"]="Ey"
colnames(dl)[colnames(dl)=="PSTACK"]="PartyID"
colnames(dl)[colnames(dl)=="RID"]="Rid"
colnames(dl)[colnames(dl)=="WEIGHT1"]="Rwd"
dl$Rwd[dl$Rwd==0]=1#missing weight put to neutral weight of 1
colnames(dl)[colnames(dl)=="TURNOUT"]="Vt"
colnames(dl)[colnames(dl)=="RECALL"]="Vn"
colnames(dl)[colnames(dl)=="LR_SELF"]="LRR"
colnames(dl)[colnames(dl)=="SYMP"]="Pl"
colnames(dl)[colnames(dl)=="LERA"]="Ll"
colnames(dl)[colnames(dl)=="LR"]="LRP"
colnames(dl)[colnames(dl)=="SIZE"]="Ps"
dl$Rage=as.numeric(NA)#Respondent Age is not coded in LP dataset
colnames(dl)[colnames(dl)=="PIDIR"]="Pi"
dl$Pi[dl$Pi==0]=NA#set 0 to NA

#keep only variables of interest:
dl=dl[,c(1,2,6,9,10,17,21,14,23,3,31,5,33,12)]


#GERMANY EV de1:----
de1=read_dta("ZA3911_de_1961-1998.dta")
colnames(de1)
colnames(de1)[colnames(de1)=="year"]="Ey"#Election Year
de1$Ey=as.numeric(de1$Ey)
de1=de1[!(de1$Ey %in% c(1969,1972)),]#get rid of elections without likability
colnames(de1)[colnames(de1)=="country"]="Ec"#Election Country
de1$Ec=as.numeric(de1$Ec)
colnames(de1)[colnames(de1)=="age"]="Rage"#Respondent Age
de1$Rage=as.numeric(de1$Rage)
de1$Rage[de1$Rage>100]=NA
#exclude East Germany in order to have West Germany as congruent electorate (and exclude Eastern Europe):
de1=de1[!(de1$Ey>1990&de1$region>10),]
#NB: in DEU_1990 even if the election included former East Germany, the survey did not
#Vt#Vote Turnout (0 did not vote; 1 voted)
colnames(de1)[colnames(de1)=="turnout"]="Vt"
de1$Vt[de1$Vt>2]=NA
de1$Vt[de1$Vt==2]=0
#"Va"#Vote Alphabetical (here numbers), by vote share:
de1$Va=NA
de1$Va[de1$Ey==1961&de1$pchoice==2]=1#1961:2,1,3
de1$Va[de1$Ey==1961&de1$pchoice==1]=2
de1$Va[de1$Ey==1961&de1$pchoice==3]=3
de1$Va[de1$Ey==1965&de1$pchoice==2]=1#1965:2,1,3
de1$Va[de1$Ey==1965&de1$pchoice==1]=2
de1$Va[de1$Ey==1965&de1$pchoice==3]=3
de1$Va[de1$Ey==1976&de1$pchoice==2]=1#1976:2,1,3
de1$Va[de1$Ey==1976&de1$pchoice==1]=2
de1$Va[de1$Ey==1976&de1$pchoice==3]=3
de1$Va[de1$Ey==1980&de1$pchoice==2]=1#1980:2,1,3
de1$Va[de1$Ey==1980&de1$pchoice==1]=2
de1$Va[de1$Ey==1980&de1$pchoice==3]=3
de1$Va[de1$Ey==1983&de1$pchoice==2]=1#1983:2,1,3
de1$Va[de1$Ey==1983&de1$pchoice==1]=2
de1$Va[de1$Ey==1983&de1$pchoice==3]=3
de1$Va[de1$Ey==1987&de1$pchoice==2]=1#1987:2,1,3
de1$Va[de1$Ey==1987&de1$pchoice==1]=2
de1$Va[de1$Ey==1987&de1$pchoice==3]=3
de1$Va[de1$Ey==1990&de1$pchoice==2]=1#1990:2,1,3,5
de1$Va[de1$Ey==1990&de1$pchoice==1]=2
de1$Va[de1$Ey==1990&de1$pchoice==3]=3
de1$Va[de1$Ey==1990&de1$pchoice==5]=4
de1$Va[de1$Ey==1994&de1$pchoice==2]=1#1994:2,1,4,3,5
de1$Va[de1$Ey==1994&de1$pchoice==1]=2
de1$Va[de1$Ey==1994&de1$pchoice==4]=3
de1$Va[de1$Ey==1994&de1$pchoice==3]=4
de1$Va[de1$Ey==1994&de1$pchoice==5]=5
de1$Va[de1$Ey==1998&de1$pchoice==1]=1#1998:1,2,4,3,5
de1$Va[de1$Ey==1998&de1$pchoice==2]=2
de1$Va[de1$Ey==1998&de1$pchoice==4]=3
de1$Va[de1$Ey==1998&de1$pchoice==3]=4
de1$Va[de1$Ey==1998&de1$pchoice==5]=5
colnames(de1)[colnames(de1)=="weight1"]="Rwd"#Respondent Weight Demographic
de1$Rwd=as.numeric(de1$Rwd)
#Number of Parties:
de1$NP=NA
de1$NP[de1$Ey==1961]=3
de1$NP[de1$Ey==1965]=3
de1$NP[de1$Ey==1969]=3
de1$NP[de1$Ey==1972]=3
de1$NP[de1$Ey==1976]=3
de1$NP[de1$Ey==1980]=3
de1$NP[de1$Ey==1983]=3
de1$NP[de1$Ey==1987]=3
de1$NP[de1$Ey==1990]=4
de1$NP[de1$Ey==1994]=5
de1$NP[de1$Ey==1998]=5
#party likability:
de1$Pl.1=NA
de1$Pl.2=NA
de1$Pl.3=NA
de1$Pl.4=NA
de1$Pl.5=NA
de1$Pl.6=NA
# de1$Pl.7=NA
de1$Pl.1[de1$Ey==1961]=de1$symp2[de1$Ey==1961]#1961:2,1,3
de1$Pl.2[de1$Ey==1961]=de1$symp1[de1$Ey==1961]
de1$Pl.3[de1$Ey==1961]=de1$symp3[de1$Ey==1961]
de1$Pl.1[de1$Ey==1965]=de1$symp2[de1$Ey==1965]#1965:2,1,3
de1$Pl.2[de1$Ey==1965]=de1$symp1[de1$Ey==1965]
de1$Pl.3[de1$Ey==1965]=de1$symp3[de1$Ey==1965]
de1$Pl.1[de1$Ey==1976]=de1$symp2[de1$Ey==1976]#1976:2,1,3
de1$Pl.2[de1$Ey==1976]=de1$symp1[de1$Ey==1976]
de1$Pl.3[de1$Ey==1976]=de1$symp3[de1$Ey==1976]
de1$Pl.1[de1$Ey==1980]=de1$symp2[de1$Ey==1980]#1980:2,1,3
de1$Pl.2[de1$Ey==1980]=de1$symp1[de1$Ey==1980]
de1$Pl.3[de1$Ey==1980]=de1$symp3[de1$Ey==1980]
de1$Pl.1[de1$Ey==1983]=de1$symp2[de1$Ey==1983]#1983:2,1,3
de1$Pl.2[de1$Ey==1983]=de1$symp1[de1$Ey==1983]
de1$Pl.3[de1$Ey==1983]=de1$symp3[de1$Ey==1983]
de1$Pl.1[de1$Ey==1987]=de1$symp2[de1$Ey==1987]#1987:2,1,3
de1$Pl.2[de1$Ey==1987]=de1$symp1[de1$Ey==1987]
de1$Pl.3[de1$Ey==1987]=de1$symp3[de1$Ey==1987]
de1$Pl.1[de1$Ey==1990]=de1$symp2[de1$Ey==1990]#1990:2,1,3,5
de1$Pl.2[de1$Ey==1990]=de1$symp1[de1$Ey==1990]
de1$Pl.3[de1$Ey==1990]=de1$symp3[de1$Ey==1990]
de1$Pl.4[de1$Ey==1990]=de1$symp5[de1$Ey==1990]
de1$Pl.1[de1$Ey==1994]=de1$symp2[de1$Ey==1994]#1994:2,1,4,3,5
de1$Pl.2[de1$Ey==1994]=de1$symp1[de1$Ey==1994]
de1$Pl.3[de1$Ey==1994]=de1$symp4[de1$Ey==1994]
de1$Pl.4[de1$Ey==1994]=de1$symp3[de1$Ey==1994]
de1$Pl.5[de1$Ey==1994]=de1$symp5[de1$Ey==1994]
de1$Pl.1[de1$Ey==1998]=de1$symp1[de1$Ey==1998]#1998:1,2,4,3,5
de1$Pl.2[de1$Ey==1998]=de1$symp2[de1$Ey==1998]
de1$Pl.3[de1$Ey==1998]=de1$symp4[de1$Ey==1998]
de1$Pl.4[de1$Ey==1998]=de1$symp3[de1$Ey==1998]
de1$Pl.5[de1$Ey==1998]=de1$symp5[de1$Ey==1998]
de1$Pl.1=de1$Pl.1-1#recoding to 0-10 scale
de1$Pl.2=de1$Pl.2-1
de1$Pl.3=de1$Pl.3-1
de1$Pl.4=de1$Pl.4-1
de1$Pl.5=de1$Pl.5-1
#leader likability:
de1$Ll.1=NA
de1$Ll.2=NA
de1$Ll.3=NA
de1$Ll.4=NA
de1$Ll.5=NA
de1$Ll.6=NA
# de1$Ll.7=NA
de1$Ll.1[de1$Ey==1961]=de1$sympl2[de1$Ey==1961]#1961:2,1,3
de1$Ll.2[de1$Ey==1961]=de1$sympl1[de1$Ey==1961]
de1$Ll.3[de1$Ey==1961]=de1$sympl3[de1$Ey==1961]
de1$Ll.1[de1$Ey==1965]=de1$sympl2[de1$Ey==1965]#1965:2,1,3
de1$Ll.2[de1$Ey==1965]=de1$sympl1[de1$Ey==1965]
de1$Ll.3[de1$Ey==1965]=de1$sympl3[de1$Ey==1965]
de1$Ll.1[de1$Ey==1976]=de1$sympl2[de1$Ey==1976]#1976:2,1,3
de1$Ll.2[de1$Ey==1976]=de1$sympl1[de1$Ey==1976]
de1$Ll.3[de1$Ey==1976]=de1$sympl3[de1$Ey==1976]
de1$Ll.1[de1$Ey==1980]=de1$sympl2[de1$Ey==1980]#1980:2,1,3
de1$Ll.2[de1$Ey==1980]=de1$sympl1[de1$Ey==1980]
de1$Ll.3[de1$Ey==1980]=de1$sympl3[de1$Ey==1980]
de1$Ll.1[de1$Ey==1983]=de1$sympl2[de1$Ey==1983]#1983:2,1,3
de1$Ll.2[de1$Ey==1983]=de1$sympl1[de1$Ey==1983]
de1$Ll.3[de1$Ey==1983]=de1$sympl3[de1$Ey==1983]
de1$Ll.1[de1$Ey==1987]=de1$sympl2[de1$Ey==1987]#1987:2,1,3
de1$Ll.2[de1$Ey==1987]=de1$sympl1[de1$Ey==1987]
de1$Ll.3[de1$Ey==1987]=de1$sympl3[de1$Ey==1987]
de1$Ll.1[de1$Ey==1990]=de1$sympl2[de1$Ey==1990]#1990:2,1,3,5
de1$Ll.2[de1$Ey==1990]=de1$sympl1[de1$Ey==1990]
de1$Ll.3[de1$Ey==1990]=de1$sympl3[de1$Ey==1990]
de1$Ll.4[de1$Ey==1990]=de1$sympl5[de1$Ey==1990]
de1$Ll.1[de1$Ey==1994]=de1$sympl2[de1$Ey==1994]#1994:2,1,4,3,5
de1$Ll.2[de1$Ey==1994]=de1$sympl1[de1$Ey==1994]
de1$Ll.3[de1$Ey==1994]=de1$sympl4[de1$Ey==1994]
de1$Ll.4[de1$Ey==1994]=de1$sympl3[de1$Ey==1994]
de1$Ll.5[de1$Ey==1994]=de1$sympl5[de1$Ey==1994]
de1$Ll.1[de1$Ey==1998]=de1$sympl1[de1$Ey==1998]#1998:1,2,4,3,5
de1$Ll.2[de1$Ey==1998]=de1$sympl2[de1$Ey==1998]
de1$Ll.3[de1$Ey==1998]=de1$sympl4[de1$Ey==1998]
de1$Ll.4[de1$Ey==1998]=de1$sympl3[de1$Ey==1998]
de1$Ll.5[de1$Ey==1998]=de1$sympl5[de1$Ey==1998]
de1$Ll.1=de1$Ll.1-1#recoding to 0-10 scale
de1$Ll.2=de1$Ll.2-1
de1$Ll.3=de1$Ll.3-1
de1$Ll.4=de1$Ll.4-1
de1$Ll.5=de1$Ll.5-1
#LRR=Left Right Respondent (voter LR self):
colnames(de1)[colnames(de1)=="lrself"]="LRR"
de1$LRR=de1$LRR-1#recoding to 0-10 scale
de1$LRR=as.numeric(de1$LRR)
#LRP.i=Left Right Parties:
de1$LRP.1=NA
de1$LRP.2=NA
de1$LRP.3=NA
de1$LRP.4=NA
de1$LRP.5=NA
de1$LRP.6=NA
# de1$LRP.7=NA
de1$LRP.1[de1$Ey==1961]=de1$lrp2[de1$Ey==1961]#1961:2,1,3
de1$LRP.2[de1$Ey==1961]=de1$lrp1[de1$Ey==1961]
de1$LRP.3[de1$Ey==1961]=de1$lrp3[de1$Ey==1961]
de1$LRP.1[de1$Ey==1965]=de1$lrp2[de1$Ey==1965]#1965:2,1,3
de1$LRP.2[de1$Ey==1965]=de1$lrp1[de1$Ey==1965]
de1$LRP.3[de1$Ey==1965]=de1$lrp3[de1$Ey==1965]
de1$LRP.1[de1$Ey==1976]=de1$lrp2[de1$Ey==1976]#1976:2,1,3
de1$LRP.2[de1$Ey==1976]=de1$lrp1[de1$Ey==1976]
de1$LRP.3[de1$Ey==1976]=de1$lrp3[de1$Ey==1976]
de1$LRP.1[de1$Ey==1980]=de1$lrp2[de1$Ey==1980]#1980:2,1,3
de1$LRP.2[de1$Ey==1980]=de1$lrp1[de1$Ey==1980]
de1$LRP.3[de1$Ey==1980]=de1$lrp3[de1$Ey==1980]
de1$LRP.1[de1$Ey==1983]=de1$lrp2[de1$Ey==1983]#1983:2,1,3
de1$LRP.2[de1$Ey==1983]=de1$lrp1[de1$Ey==1983]
de1$LRP.3[de1$Ey==1983]=de1$lrp3[de1$Ey==1983]
de1$LRP.1[de1$Ey==1987]=de1$lrp2[de1$Ey==1987]#1987:2,1,3
de1$LRP.2[de1$Ey==1987]=de1$lrp1[de1$Ey==1987]
de1$LRP.3[de1$Ey==1987]=de1$lrp3[de1$Ey==1987]
de1$LRP.1[de1$Ey==1990]=de1$lrp2[de1$Ey==1990]#1990:2,1,3,5
de1$LRP.2[de1$Ey==1990]=de1$lrp1[de1$Ey==1990]
de1$LRP.3[de1$Ey==1990]=de1$lrp3[de1$Ey==1990]
de1$LRP.4[de1$Ey==1990]=de1$lrp5[de1$Ey==1990]
de1$LRP.1[de1$Ey==1994]=de1$lrp2[de1$Ey==1994]#1994:2,1,4,3,5
de1$LRP.2[de1$Ey==1994]=de1$lrp1[de1$Ey==1994]
de1$LRP.3[de1$Ey==1994]=de1$lrp4[de1$Ey==1994]
de1$LRP.4[de1$Ey==1994]=de1$lrp3[de1$Ey==1994]
de1$LRP.5[de1$Ey==1994]=de1$lrp5[de1$Ey==1994]
de1$LRP.1[de1$Ey==1998]=de1$lrp1[de1$Ey==1998]#1998:1,2,4,3,5
de1$LRP.2[de1$Ey==1998]=de1$lrp2[de1$Ey==1998]
de1$LRP.3[de1$Ey==1998]=de1$lrp4[de1$Ey==1998]
de1$LRP.4[de1$Ey==1998]=de1$lrp3[de1$Ey==1998]
de1$LRP.5[de1$Ey==1998]=de1$lrp5[de1$Ey==1998]
de1$LRP.1=de1$LRP.1-1#recoding to 0-10 scale
de1$LRP.2=de1$LRP.2-1
de1$LRP.3=de1$LRP.3-1
de1$LRP.4=de1$LRP.4-1
de1$LRP.5=de1$LRP.5-1
#Elections with LRR and LRP:
de1$LR=NA
de1$LR[de1$Ey==1961]=0
de1$LR[de1$Ey==1965]=0
de1$LR[de1$Ey==1969]=1
de1$LR[de1$Ey==1972]=1
de1$LR[de1$Ey==1976]=1
de1$LR[de1$Ey==1980]=0
de1$LR[de1$Ey==1983]=1
de1$LR[de1$Ey==1987]=1
de1$LR[de1$Ey==1990]=1
de1$LR[de1$Ey==1994]=0
de1$LR[de1$Ey==1998]=1
#EeconS=Election economy state Subjective:(Retrospective socio-tropic economic evaluations)
# 1. MUCH BETTER                 
# 2. SOMEWHAT BETTER     
# 3. STAYED THE SAME              
# 4. SOMEWHAT WORSE
# 5. MUCH WORSE
colnames(de1)[colnames(de1)=="economy1"]="EeconS"
#recode EeconS from EV scale
# 1. GOTTEN BETTER c(1,2,3,4,5)
# 5. GOTTEN WORSE 
# to TEV scale, noting that the TEV used the conversion scale below for same elections:
# 0. Negative evaluation c(0, 0.25, 0.5, 0.75, 1)
# 1. Positive evaluation
de1$EeconS[de1$EeconS==1]=1
de1$EeconS[de1$EeconS==2]=0.75
de1$EeconS[de1$EeconS==3]=0.5
de1$EeconS[de1$EeconS==4]=0.25
de1$EeconS[de1$EeconS==5]=0
#Elections with EeconS:
de1$Eecon=NA
de1$Eecon[de1$Ey==1961]=1
de1$Eecon[de1$Ey==1965]=0
de1$Eecon[de1$Ey==1969]=1
de1$Eecon[de1$Ey==1972]=1
de1$Eecon[de1$Ey==1976]=1
de1$Eecon[de1$Ey==1980]=0
de1$Eecon[de1$Ey==1983]=1
de1$Eecon[de1$Ey==1987]=1
de1$Eecon[de1$Ey==1990]=1
de1$Eecon[de1$Ey==1994]=1
de1$Eecon[de1$Ey==1998]=0
#Seats to vote ratios:
de1$P1P2rat=NA
de1$P1P2rat[de1$Ey==1961]=0.99
de1$P1P2rat[de1$Ey==1965]=0.95
de1$P1P2rat[de1$Ey==1969]=0.98
de1$P1P2rat[de1$Ey==1972]=1.01
de1$P1P2rat[de1$Ey==1976]=0.99
de1$P1P2rat[de1$Ey==1980]=1.00
de1$P1P2rat[de1$Ey==1983]=0.99
de1$P1P2rat[de1$Ey==1987]=1.01
de1$P1P2rat[de1$Ey==1990]=1.02
de1$P1P2rat[de1$Ey==1994]=1.03
de1$P1P2rat[de1$Ey==1998]=1.04
#Gallagher Disproportionality measure:
de1$GDM=NA
de1$GDM[de1$Ey==1961]=3.43
de1$GDM[de1$Ey==1965]=2.31
de1$GDM[de1$Ey==1969]=3.92
de1$GDM[de1$Ey==1972]=0.67
de1$GDM[de1$Ey==1976]=0.59
de1$GDM[de1$Ey==1980]=1.41
de1$GDM[de1$Ey==1983]=0.50
de1$GDM[de1$Ey==1987]=0.76
de1$GDM[de1$Ey==1990]=4.63
de1$GDM[de1$Ey==1994]=2.22
de1$GDM[de1$Ey==1998]=3.15
#Party family:
de1$PartyFamilyCode1=30
de1$PartyFamilyCode2=50
de1$PartyFamilyCode3=40
de1$PartyFamilyCode4=10
de1$PartyFamilyCode5=20
de1$Pf.1=NA
de1$Pf.2=NA
de1$Pf.3=NA
de1$Pf.4=NA
de1$Pf.5=NA
de1$Pf.6=NA
# de1$Pf.7=NA
de1$Pf.1[de1$Ey==1961]=de1$PartyFamilyCode2[de1$Ey==1961]#1961:2,1,3
de1$Pf.2[de1$Ey==1961]=de1$PartyFamilyCode1[de1$Ey==1961]
de1$Pf.3[de1$Ey==1961]=de1$PartyFamilyCode3[de1$Ey==1961]
de1$Pf.1[de1$Ey==1965]=de1$PartyFamilyCode2[de1$Ey==1965]#1965:2,1,3
de1$Pf.2[de1$Ey==1965]=de1$PartyFamilyCode1[de1$Ey==1965]
de1$Pf.3[de1$Ey==1965]=de1$PartyFamilyCode3[de1$Ey==1965]
de1$Pf.1[de1$Ey==1976]=de1$PartyFamilyCode2[de1$Ey==1976]#1976:2,1,3
de1$Pf.2[de1$Ey==1976]=de1$PartyFamilyCode1[de1$Ey==1976]
de1$Pf.3[de1$Ey==1976]=de1$PartyFamilyCode3[de1$Ey==1976]
de1$Pf.1[de1$Ey==1980]=de1$PartyFamilyCode2[de1$Ey==1980]#1980:2,1,3
de1$Pf.2[de1$Ey==1980]=de1$PartyFamilyCode1[de1$Ey==1980]
de1$Pf.3[de1$Ey==1980]=de1$PartyFamilyCode3[de1$Ey==1980]
de1$Pf.1[de1$Ey==1983]=de1$PartyFamilyCode2[de1$Ey==1983]#1983:2,1,3
de1$Pf.2[de1$Ey==1983]=de1$PartyFamilyCode1[de1$Ey==1983]
de1$Pf.3[de1$Ey==1983]=de1$PartyFamilyCode3[de1$Ey==1983]
de1$Pf.1[de1$Ey==1987]=de1$PartyFamilyCode2[de1$Ey==1987]#1987:2,1,3
de1$Pf.2[de1$Ey==1987]=de1$PartyFamilyCode1[de1$Ey==1987]
de1$Pf.3[de1$Ey==1987]=de1$PartyFamilyCode3[de1$Ey==1987]
de1$Pf.1[de1$Ey==1990]=de1$PartyFamilyCode2[de1$Ey==1990]#1990:2,1,3,5
de1$Pf.2[de1$Ey==1990]=de1$PartyFamilyCode1[de1$Ey==1990]
de1$Pf.3[de1$Ey==1990]=de1$PartyFamilyCode3[de1$Ey==1990]
de1$Pf.4[de1$Ey==1990]=de1$PartyFamilyCode5[de1$Ey==1990]
de1$Pf.1[de1$Ey==1994]=de1$PartyFamilyCode2[de1$Ey==1994]#1994:2,1,4,3,5
de1$Pf.2[de1$Ey==1994]=de1$PartyFamilyCode1[de1$Ey==1994]
de1$Pf.3[de1$Ey==1994]=de1$PartyFamilyCode4[de1$Ey==1994]
de1$Pf.4[de1$Ey==1994]=de1$PartyFamilyCode3[de1$Ey==1994]
de1$Pf.5[de1$Ey==1994]=de1$PartyFamilyCode5[de1$Ey==1994]
de1$Pf.1[de1$Ey==1998]=de1$PartyFamilyCode1[de1$Ey==1998]#1998:1,2,4,3,5
de1$Pf.2[de1$Ey==1998]=de1$PartyFamilyCode2[de1$Ey==1998]
de1$Pf.3[de1$Ey==1998]=de1$PartyFamilyCode4[de1$Ey==1998]
de1$Pf.4[de1$Ey==1998]=de1$PartyFamilyCode3[de1$Ey==1998]
de1$Pf.5[de1$Ey==1998]=de1$PartyFamilyCode5[de1$Ey==1998]
#coding coalition status:
de1$CoalCode=1
#CMPid.i:
de1$CMPid.1=NA#create CMPid:
de1$CMPid.2=NA
de1$CMPid.3=NA
de1$CMPid.4=NA
de1$CMPid.5=NA
de1$CMPid.6=NA
de1$CMPid.1=as.numeric(de1$CMPid.1)#recode as numeric:
de1$CMPid.2=as.numeric(de1$CMPid.2)
de1$CMPid.3=as.numeric(de1$CMPid.3)
de1$CMPid.4=as.numeric(de1$CMPid.4)
de1$CMPid.5=as.numeric(de1$CMPid.5)
de1$CMPid.6=as.numeric(de1$CMPid.6)
#PrePost:
de1$PrePost=as.numeric(NA)
#"Pi"#Party identification:
#(there is not for 1961,1965)
de1$Pi=NA
de1$Pi[de1$Ey==1976&de1$pid==2]=1#1976:2,1,3
de1$Pi[de1$Ey==1976&de1$pid==1]=2
de1$Pi[de1$Ey==1976&de1$pid==3]=3
de1$Pi[de1$Ey==1980&de1$pid==2]=1#1980:2,1,3
de1$Pi[de1$Ey==1980&de1$pid==1]=2
de1$Pi[de1$Ey==1980&de1$pid==3]=3
de1$Pi[de1$Ey==1983&de1$pid==2]=1#1983:2,1,3
de1$Pi[de1$Ey==1983&de1$pid==1]=2
de1$Pi[de1$Ey==1983&de1$pid==3]=3
de1$Pi[de1$Ey==1987&de1$pid==2]=1#1987:2,1,3
de1$Pi[de1$Ey==1987&de1$pid==1]=2
de1$Pi[de1$Ey==1987&de1$pid==3]=3
de1$Pi[de1$Ey==1990&de1$pid==2]=1#1990:2,1,3,5
de1$Pi[de1$Ey==1990&de1$pid==1]=2
de1$Pi[de1$Ey==1990&de1$pid==3]=3
de1$Pi[de1$Ey==1990&de1$pid==5]=4
de1$Pi[de1$Ey==1994&de1$pid==2]=1#1994:2,1,4,3,5
de1$Pi[de1$Ey==1994&de1$pid==1]=2
de1$Pi[de1$Ey==1994&de1$pid==4]=3
de1$Pi[de1$Ey==1994&de1$pid==3]=4
de1$Pi[de1$Ey==1994&de1$pid==5]=5
de1$Pi[de1$Ey==1998&de1$pid==1]=1#1998:1,2,4,3,5
de1$Pi[de1$Ey==1998&de1$pid==2]=2
de1$Pi[de1$Ey==1998&de1$pid==4]=3
de1$Pi[de1$Ey==1998&de1$pid==3]=4
de1$Pi[de1$Ey==1998&de1$pid==5]=5
#subset:
colnames(de1)
de1=de1[,c(1,2,3,138,127,137,139:150,157,52,151:156,158,58,159,160,166:179,11,180)]
colnames(de1)
de1[colnames(de1)]=sapply(de1[colnames(de1)],as.numeric)


#DENMARK EV de2:----
de2=read_dta("ZA3911_dk_1971-1998.dta")
colnames(de2)
colnames(de2)[colnames(de2)=="year"]="Ey"#Election Year
de2=de2[de2$Ey %in% c(1971,1973,1994,1998),]#get rid of elections without likability
colnames(de2)[colnames(de2)=="country"]="Ec"#Election Country
de2$Ec=as.numeric(de2$Ec)
colnames(de2)[colnames(de2)=="age"]="Rage"#Respondent Age
de2$Rage=as.numeric(de2$Rage)
de2$Rage[de2$Rage>100]=NA
#Vt#Vote Turnout (0 did not vote; 1 voted)
colnames(de2)[colnames(de2)=="turnout"]="Vt"
de2$Vt[de2$Vt>2]=NA
de2$Vt[de2$Vt==2]=0
#"Va"#Vote Alphabetical (here numbers), by vote share:
de2$Va=NA
de2$Va[de2$Ey==1971&de2$vote==1]=1#1971:1,3,22,2,6,17
de2$Va[de2$Ey==1971&de2$vote==3]=2
de2$Va[de2$Ey==1971&de2$vote==22]=3
de2$Va[de2$Ey==1971&de2$vote==2]=4
de2$Va[de2$Ey==1971&de2$vote==6]=5
de2$Va[de2$Ey==1971&de2$vote==17]=6
de2$Va[de2$Ey==1973&de2$vote==1]=1#1973:1,26,22,2,3,4
de2$Va[de2$Ey==1973&de2$vote==26]=2
de2$Va[de2$Ey==1973&de2$vote==22]=3
de2$Va[de2$Ey==1973&de2$vote==2]=4
de2$Va[de2$Ey==1973&de2$vote==3]=5
de2$Va[de2$Ey==1973&de2$vote==4]=6
de2$Va[de2$Ey==1994&de2$vote==1]=1#1994:1,22,3,6,26,2
de2$Va[de2$Ey==1994&de2$vote==22]=2
de2$Va[de2$Ey==1994&de2$vote==3]=3
de2$Va[de2$Ey==1994&de2$vote==6]=4
de2$Va[de2$Ey==1994&de2$vote==26]=5
de2$Va[de2$Ey==1994&de2$vote==2]=6
de2$Va[de2$Ey==1998&de2$vote==1]=1#1998:1,22,3,6,(15),4,2
de2$Va[de2$Ey==1998&de2$vote==22]=2
de2$Va[de2$Ey==1998&de2$vote==3]=3
de2$Va[de2$Ey==1998&de2$vote==6]=4
de2$Va[de2$Ey==1998&de2$vote==4]=5
de2$Va[de2$Ey==1998&de2$vote==2]=6
colnames(de2)[colnames(de2)=="weight"]="Rwd"#Respondent Weight Demographic (there's no weight, all entries are 1)
de2$Rwd=1#no info on weights
#Number of Parties:
de2$NP=NA
de2$NP[de2$Ey==1971]=6
de2$NP[de2$Ey==1973]=6
de2$NP[de2$Ey==1994]=6
de2$NP[de2$Ey==1998]=6
#party likability:
de2$Pl.1=NA
de2$Pl.2=NA
de2$Pl.3=NA
de2$Pl.4=NA
de2$Pl.5=NA
de2$Pl.6=NA
# de2$Pl.7=NA
de2$Pl.1[de2$Ey==1971]=de2$symp1[de2$Ey==1971]#1971:1,3,22,2,6,17
de2$Pl.2[de2$Ey==1971]=de2$symp3[de2$Ey==1971]
de2$Pl.3[de2$Ey==1971]=de2$symp22[de2$Ey==1971]
de2$Pl.4[de2$Ey==1971]=de2$symp2[de2$Ey==1971]
de2$Pl.5[de2$Ey==1971]=de2$symp6[de2$Ey==1971]
de2$Pl.6[de2$Ey==1971]=de2$symp17[de2$Ey==1971]
de2$Pl.1[de2$Ey==1973]=de2$symp1[de2$Ey==1973]#1973:1,26,22,2,3,4
de2$Pl.2[de2$Ey==1973]=de2$symp26[de2$Ey==1973]
de2$Pl.3[de2$Ey==1973]=de2$symp22[de2$Ey==1973]
de2$Pl.4[de2$Ey==1973]=de2$symp2[de2$Ey==1973]
de2$Pl.5[de2$Ey==1973]=de2$symp3[de2$Ey==1973]
de2$Pl.6[de2$Ey==1973]=de2$symp4[de2$Ey==1973]
de2$Pl.1[de2$Ey==1994]=de2$symp1[de2$Ey==1994]#1994:1,22,3,6,26,2
de2$Pl.2[de2$Ey==1994]=de2$symp22[de2$Ey==1994]
de2$Pl.3[de2$Ey==1994]=de2$symp3[de2$Ey==1994]
de2$Pl.4[de2$Ey==1994]=de2$symp6[de2$Ey==1994]
de2$Pl.5[de2$Ey==1994]=de2$symp26[de2$Ey==1994]
de2$Pl.6[de2$Ey==1994]=de2$symp2[de2$Ey==1994]
de2$Pl.1[de2$Ey==1998]=de2$symp1[de2$Ey==1998]#1998:1,22,3,6,(15),4,2
de2$Pl.2[de2$Ey==1998]=de2$symp22[de2$Ey==1998]
de2$Pl.3[de2$Ey==1998]=de2$symp3[de2$Ey==1998]
de2$Pl.4[de2$Ey==1998]=de2$symp6[de2$Ey==1998]
de2$Pl.5[de2$Ey==1998]=de2$symp4[de2$Ey==1998]
de2$Pl.6[de2$Ey==1998]=de2$symp2[de2$Ey==1998]
de2$Pl.1[de2$Pl.1>10]=NA#recoding to 0-10 scale
de2$Pl.2[de2$Pl.2>10]=NA
de2$Pl.3[de2$Pl.3>10]=NA
de2$Pl.4[de2$Pl.4>10]=NA
de2$Pl.5[de2$Pl.5>10]=NA
de2$Pl.6[de2$Pl.6>10]=NA
#leader likability:
de2$Ll.1=NA
de2$Ll.2=NA
de2$Ll.3=NA
de2$Ll.4=NA
de2$Ll.5=NA
de2$Ll.6=NA
# de2$Ll.7=NA
de2$Ll.1[de2$Ey==1971]=de2$sympl1[de2$Ey==1971]#1971:1,3,22,2,6,17
de2$Ll.2[de2$Ey==1971]=de2$sympl3[de2$Ey==1971]
de2$Ll.3[de2$Ey==1971]=de2$sympl22[de2$Ey==1971]
de2$Ll.4[de2$Ey==1971]=de2$sympl2[de2$Ey==1971]
de2$Ll.5[de2$Ey==1971]=de2$sympl6[de2$Ey==1971]
de2$Ll.6[de2$Ey==1971]=de2$sympl17[de2$Ey==1971]
de2$Ll.1[de2$Ey==1973]=de2$sympl1[de2$Ey==1973]#1973:1,26,22,2,3,4
de2$Ll.2[de2$Ey==1973]=de2$sympl26[de2$Ey==1973]
de2$Ll.3[de2$Ey==1973]=de2$sympl22[de2$Ey==1973]
de2$Ll.4[de2$Ey==1973]=de2$sympl2[de2$Ey==1973]
de2$Ll.5[de2$Ey==1973]=de2$sympl3[de2$Ey==1973]
de2$Ll.6[de2$Ey==1973]=de2$sympl4[de2$Ey==1973]
de2$Ll.1[de2$Ey==1994]=de2$sympl1[de2$Ey==1994]#1994:1,22,3,6,26,2
de2$Ll.2[de2$Ey==1994]=de2$sympl22[de2$Ey==1994]
de2$Ll.3[de2$Ey==1994]=de2$sympl3[de2$Ey==1994]
de2$Ll.4[de2$Ey==1994]=de2$sympl6[de2$Ey==1994]
de2$Ll.5[de2$Ey==1994]=de2$sympl26[de2$Ey==1994]
de2$Ll.6[de2$Ey==1994]=de2$sympl2[de2$Ey==1994]
de2$Ll.1[de2$Ey==1998]=de2$sympl1[de2$Ey==1998]#1998:1,22,3,6,(15),4,2
de2$Ll.2[de2$Ey==1998]=de2$sympl22[de2$Ey==1998]
de2$Ll.3[de2$Ey==1998]=de2$sympl3[de2$Ey==1998]
de2$Ll.4[de2$Ey==1998]=de2$sympl6[de2$Ey==1998]
de2$Ll.5[de2$Ey==1998]=de2$sympl4[de2$Ey==1998]
de2$Ll.6[de2$Ey==1998]=de2$sympl2[de2$Ey==1998]
de2$Ll.1[de2$Ll.1>10]=NA#recoding to 0-10 scale
de2$Ll.2[de2$Ll.2>10]=NA
de2$Ll.3[de2$Ll.3>10]=NA
de2$Ll.4[de2$Ll.4>10]=NA
de2$Ll.5[de2$Ll.5>10]=NA
de2$Ll.6[de2$Ll.6>10]=NA
#LRR=Left Right Respondent (voter LR self):
colnames(de2)[colnames(de2)=="lrsp"]="LRR"
de2$LRR=(de2$LRR-1)*10/9#recoding to 0-10 scale
de2$LRR[de2$LRR>10]=NA
de2$LRR=as.numeric(de2$LRR)
#LRP.i=Left Right Parties:
de2$LRP.1=NA
de2$LRP.2=NA
de2$LRP.3=NA
de2$LRP.4=NA
de2$LRP.5=NA
de2$LRP.6=NA
# de2$LRP.7=NA
de2$LRP.1[de2$Ey==1971]=de2$lrp1[de2$Ey==1971]#1971:1,3,22,2,6,17
de2$LRP.2[de2$Ey==1971]=de2$lrp3[de2$Ey==1971]
de2$LRP.3[de2$Ey==1971]=de2$lrp22[de2$Ey==1971]
de2$LRP.4[de2$Ey==1971]=de2$lrp2[de2$Ey==1971]
de2$LRP.5[de2$Ey==1971]=de2$lrp6[de2$Ey==1971]
de2$LRP.6[de2$Ey==1971]=de2$lrp17[de2$Ey==1971]
de2$LRP.1[de2$Ey==1973]=de2$lrp1[de2$Ey==1973]#1973:1,26,22,2,3,4
de2$LRP.2[de2$Ey==1973]=de2$lrp26[de2$Ey==1973]
de2$LRP.3[de2$Ey==1973]=de2$lrp22[de2$Ey==1973]
de2$LRP.4[de2$Ey==1973]=de2$lrp2[de2$Ey==1973]
de2$LRP.5[de2$Ey==1973]=de2$lrp3[de2$Ey==1973]
de2$LRP.6[de2$Ey==1973]=de2$lrp4[de2$Ey==1973]
de2$LRP.1[de2$Ey==1994]=de2$lrp1[de2$Ey==1994]#1994:1,22,3,6,26,2
de2$LRP.2[de2$Ey==1994]=de2$lrp22[de2$Ey==1994]
de2$LRP.3[de2$Ey==1994]=de2$lrp3[de2$Ey==1994]
de2$LRP.4[de2$Ey==1994]=de2$lrp6[de2$Ey==1994]
de2$LRP.5[de2$Ey==1994]=de2$lrp26[de2$Ey==1994]
de2$LRP.6[de2$Ey==1994]=de2$lrp2[de2$Ey==1994]
de2$LRP.1[de2$Ey==1998]=de2$lrp1[de2$Ey==1998]#1998:1,22,3,6,(15),4,2
de2$LRP.2[de2$Ey==1998]=de2$lrp22[de2$Ey==1998]
de2$LRP.3[de2$Ey==1998]=de2$lrp3[de2$Ey==1998]
de2$LRP.4[de2$Ey==1998]=de2$lrp6[de2$Ey==1998]
de2$LRP.5[de2$Ey==1998]=de2$lrp4[de2$Ey==1998]
de2$LRP.6[de2$Ey==1998]=de2$lrp2[de2$Ey==1998]
de2$LRP.1=(de2$LRP.1-1)*10/9#recoding to 0-10 scale
de2$LRP.1[de2$LRP.1>10]=NA
de2$LRP.2=(de2$LRP.2-1)*10/9
de2$LRP.2[de2$LRP.2>10]=NA
de2$LRP.3=(de2$LRP.3-1)*10/9
de2$LRP.3[de2$LRP.3>10]=NA
de2$LRP.4=(de2$LRP.4-1)*10/9
de2$LRP.4[de2$LRP.4>10]=NA
de2$LRP.5=(de2$LRP.5-1)*10/9
de2$LRP.5[de2$LRP.5>10]=NA
de2$LRP.6=(de2$LRP.6-1)*10/9
de2$LRP.6[de2$LRP.6>10]=NA
#Elections with LRR and LRP:
de2$LR=NA
de2$LR[de2$Ey==1971]=0
de2$LR[de2$Ey==1973]=0
de2$LR[de2$Ey==1994]=1
de2$LR[de2$Ey==1998]=1
#EeconS=Election economy state Subjective:(Retrospective socio-tropic economic evaluations)
de2$EeconS=NA#no data on economy state subjective
#Elections with EeconS:
de2$Eecon=0#(no data)
#Seats to vote ratios:
de2$P1P2rat=NA
de2$P1P2rat[de2$Ey==1971]=1.01
de2$P1P2rat[de2$Ey==1973]=1.02
de2$P1P2rat[de2$Ey==1994]=0.99
de2$P1P2rat[de2$Ey==1998]=1.00
#Gallagher Disproportionality measure:
de2$GDM=NA
de2$GDM[de2$Ey==1971]=3.45
de2$GDM[de2$Ey==1973]=1.20
de2$GDM[de2$Ey==1994]=1.57
de2$GDM[de2$Ey==1998]=0.42
#Party family:
de2$PartyFamilyCode1=30
de2$PartyFamilyCode2=40
de2$PartyFamilyCode3=60
de2$PartyFamilyCode4=60
de2$PartyFamilyCode6=20
de2$PartyFamilyCode17=50
de2$PartyFamilyCode22=40
de2$PartyFamilyCode26=70
de2$Pf.1=NA
de2$Pf.2=NA
de2$Pf.3=NA
de2$Pf.4=NA
de2$Pf.5=NA
de2$Pf.6=NA
# de2$Pf.7=NA
de2$Pf.1[de2$Ey==1971]=de2$PartyFamilyCode1[de2$Ey==1971]#1971:1,3,22,2,6,17
de2$Pf.2[de2$Ey==1971]=de2$PartyFamilyCode3[de2$Ey==1971]
de2$Pf.3[de2$Ey==1971]=de2$PartyFamilyCode22[de2$Ey==1971]
de2$Pf.4[de2$Ey==1971]=de2$PartyFamilyCode2[de2$Ey==1971]
de2$Pf.5[de2$Ey==1971]=de2$PartyFamilyCode6[de2$Ey==1971]
de2$Pf.6[de2$Ey==1971]=de2$PartyFamilyCode17[de2$Ey==1971]
de2$Pf.1[de2$Ey==1973]=de2$PartyFamilyCode1[de2$Ey==1973]#1973:1,26,22,2,3,4
de2$Pf.2[de2$Ey==1973]=de2$PartyFamilyCode26[de2$Ey==1973]
de2$Pf.3[de2$Ey==1973]=de2$PartyFamilyCode22[de2$Ey==1973]
de2$Pf.4[de2$Ey==1973]=de2$PartyFamilyCode2[de2$Ey==1973]
de2$Pf.5[de2$Ey==1973]=de2$PartyFamilyCode3[de2$Ey==1973]
de2$Pf.6[de2$Ey==1973]=de2$PartyFamilyCode4[de2$Ey==1973]
de2$Pf.1[de2$Ey==1994]=de2$PartyFamilyCode1[de2$Ey==1994]#1994:1,22,3,6,26,2
de2$Pf.2[de2$Ey==1994]=de2$PartyFamilyCode22[de2$Ey==1994]
de2$Pf.3[de2$Ey==1994]=de2$PartyFamilyCode3[de2$Ey==1994]
de2$Pf.4[de2$Ey==1994]=de2$PartyFamilyCode6[de2$Ey==1994]
de2$Pf.5[de2$Ey==1994]=de2$PartyFamilyCode26[de2$Ey==1994]
de2$Pf.6[de2$Ey==1994]=de2$PartyFamilyCode2[de2$Ey==1994]
de2$Pf.1[de2$Ey==1998]=de2$PartyFamilyCode1[de2$Ey==1998]#1998:1,22,3,6,(15),4,2
de2$Pf.2[de2$Ey==1998]=de2$PartyFamilyCode22[de2$Ey==1998]
de2$Pf.3[de2$Ey==1998]=de2$PartyFamilyCode3[de2$Ey==1998]
de2$Pf.4[de2$Ey==1998]=de2$PartyFamilyCode6[de2$Ey==1998]
de2$Pf.5[de2$Ey==1998]=de2$PartyFamilyCode4[de2$Ey==1998]
de2$Pf.6[de2$Ey==1998]=de2$PartyFamilyCode2[de2$Ey==1998]
#coding coalition status:
de2$CoalCode=0
#CMPid.i:
de2$CMPid.1=NA#create CMPid:
de2$CMPid.2=NA
de2$CMPid.3=NA
de2$CMPid.4=NA
de2$CMPid.5=NA
de2$CMPid.6=NA
de2$CMPid.1[de2$Ey==1971]=13320#assign CMPid by party size order for parties with likability:
de2$CMPid.2[de2$Ey==1971]=13620
de2$CMPid.3[de2$Ey==1971]=13420
de2$CMPid.4[de2$Ey==1971]=13410
de2$CMPid.5[de2$Ey==1971]=13230
de2$CMPid.6[de2$Ey==1971]=13520
de2$CMPid.1[de2$Ey==1973]=13320
de2$CMPid.2[de2$Ey==1973]=13951
de2$CMPid.3[de2$Ey==1973]=13420
de2$CMPid.4[de2$Ey==1973]=13410
de2$CMPid.5[de2$Ey==1973]=13620
de2$CMPid.6[de2$Ey==1973]=13330
de2$CMPid.1[de2$Ey==1994]=13320
de2$CMPid.2[de2$Ey==1994]=13420
de2$CMPid.3[de2$Ey==1994]=13620
de2$CMPid.4[de2$Ey==1994]=13230
de2$CMPid.5[de2$Ey==1994]=13951
de2$CMPid.6[de2$Ey==1994]=13410
de2$CMPid.1=as.numeric(de2$CMPid.1)#recode as numeric:
de2$CMPid.2=as.numeric(de2$CMPid.2)
de2$CMPid.3=as.numeric(de2$CMPid.3)
de2$CMPid.4=as.numeric(de2$CMPid.4)
de2$CMPid.5=as.numeric(de2$CMPid.5)
de2$CMPid.6=as.numeric(de2$CMPid.6)
#PrePost:
de2$PrePost=as.numeric(NA)
#"Pi"#Party identification:
#(there is not for 1971)
de2$Pi=NA
de2$Pi[de2$Ey==1973&de2$pida==1]=1#1973:1,26,22,2,3,4
de2$Pi[de2$Ey==1973&de2$pida==26]=2
de2$Pi[de2$Ey==1973&de2$pida==22]=3
de2$Pi[de2$Ey==1973&de2$pida==2]=4
de2$Pi[de2$Ey==1973&de2$pida==3]=5
de2$Pi[de2$Ey==1973&de2$pida==4]=6
de2$Pi[de2$Ey==1994&de2$pida==1]=1#1994:1,22,3,6,26,2
de2$Pi[de2$Ey==1994&de2$pida==22]=2
de2$Pi[de2$Ey==1994&de2$pida==3]=3
de2$Pi[de2$Ey==1994&de2$pida==6]=4
de2$Pi[de2$Ey==1994&de2$pida==26]=5
de2$Pi[de2$Ey==1994&de2$pida==2]=6
de2$Pi[de2$Ey==1998&de2$pida==1]=1#1998:1,22,3,6,(15),4,2
de2$Pi[de2$Ey==1998&de2$pida==22]=2
de2$Pi[de2$Ey==1998&de2$pida==3]=3
de2$Pi[de2$Ey==1998&de2$pida==6]=4
de2$Pi[de2$Ey==1998&de2$pida==4]=5
de2$Pi[de2$Ey==1998&de2$pida==2]=6
#subset:
colnames(de2)
de2=de2[,c(1,2,4,139,91,138,140:151,158,70,152:157,160,159,161,162,171:184,7,185)]
colnames(de2)
de2[colnames(de2)]=sapply(de2[colnames(de2)],as.numeric)
    
    
#GREAT BRITAIN EV de3:----
#first merge datasets:
#de31=read_dta("ZA3911_gb_1964.dta")#no likability
#de32=read_dta("ZA3911_gb_1966.dta")#no likability
#de33=read_dta("ZA3911_gb_1970.dta")#no likability
de34=read_dta("ZA3911_gb_1974f.dta")
de35=read_dta("ZA3911_gb_1974o.dta")
de35$year=1975#temporarily modifying 1974o year to code all the variables with
#unique election-year, it will be modified before the analysis
de36=read_dta("ZA3911_gb_1979.dta")
#de37=read_dta("ZA3911_gb_1983.dta")#different likability scale (and vote for other patries not coded)
#de38=read_dta("ZA3911_gb_1987.dta")#different likability scale (and leader Vs party)
#de39=read_dta("ZA3911_gb_1992.dta")#different likability scale (and leader Vs party)
de310=read_dta("ZA3911_gb_1997.dta")
de311=read_dta("ZA3911_gb_2001.dta")
de3=rbind(de34,de35,de36,de310,de311)
rm(de34,de35,de36,de310,de311)
colnames(de3)
colnames(de3)[colnames(de3)=="year"]="Ey"#Election Year
de3$Ey=as.numeric(de3$Ey)
colnames(de3)[colnames(de3)=="country"]="Ec"#Election Country
de3$Ec=as.numeric(de3$Ec)
colnames(de3)[colnames(de3)=="age"]="Rage"#Respondent Age
de3$Rage=as.numeric(de3$Rage)
de3$Rage[de3$Rage>100]=NA
#Vt#Vote Turnout (0 did not vote; 1 voted)
colnames(de3)[colnames(de3)=="turnout"]="Vt"
de3$Vt[de3$Vt>2]=NA
#"Va"#Vote Alphabetical (here numbers), by vote share:
de3$Va=NA
de3$Va[de3$Ey==1974&de3$vote==2]=1#1974:2,1,3
de3$Va[de3$Ey==1974&de3$vote==1]=2
de3$Va[de3$Ey==1974&de3$vote==3]=3
de3$Va[de3$Ey==1975&de3$vote==2]=1#1975:2,1,3
de3$Va[de3$Ey==1975&de3$vote==1]=2
de3$Va[de3$Ey==1975&de3$vote==3]=3
de3$Va[de3$Ey==1979&de3$vote==1]=1#1979:1,2,3
de3$Va[de3$Ey==1979&de3$vote==2]=2
de3$Va[de3$Ey==1979&de3$vote==3]=3
de3$Va[de3$Ey==1997&de3$vote==2]=1#1997:2,1,3
de3$Va[de3$Ey==1997&de3$vote==1]=2
de3$Va[de3$Ey==1997&de3$vote==3]=3
de3$Va[de3$Ey==2001&de3$vote==2]=1#2001:2,1,3
de3$Va[de3$Ey==2001&de3$vote==1]=2
de3$Va[de3$Ey==2001&de3$vote==3]=3
colnames(de3)[colnames(de3)=="weight"]="Rwd"#Respondent Weight Demographic
de3$Rwd=as.numeric(de3$Rwd)
de3$Rwd[de3$Rwd==0]=1#missing weight put to neutral weight of 1
#Number of Parties:
de3$NP=NA
de3$NP[de3$Ey==1974]=3
de3$NP[de3$Ey==1975]=3
de3$NP[de3$Ey==1979]=3
de3$NP[de3$Ey==1997]=3
de3$NP[de3$Ey==2001]=3
#party likability:
de3$Pl.1=NA
de3$Pl.2=NA
de3$Pl.3=NA
de3$Pl.4=NA
de3$Pl.5=NA
de3$Pl.6=NA
de3$Pl.1[de3$Ey==1974]=de3$symp2[de3$Ey==1974]#1974:2,1,3
de3$Pl.2[de3$Ey==1974]=de3$symp1[de3$Ey==1974]
de3$Pl.3[de3$Ey==1974]=de3$symp3[de3$Ey==1974]
de3$Pl.1[de3$Ey==1975]=de3$symp2[de3$Ey==1975]#1975:2,1,3
de3$Pl.2[de3$Ey==1975]=de3$symp1[de3$Ey==1975]
de3$Pl.3[de3$Ey==1975]=de3$symp3[de3$Ey==1975]
de3$Pl.1[de3$Ey==1979]=de3$symp1[de3$Ey==1979]#1979:1,2,3
de3$Pl.2[de3$Ey==1979]=de3$symp2[de3$Ey==1979]
de3$Pl.3[de3$Ey==1979]=de3$symp3[de3$Ey==1979]
de3$Pl.1[de3$Ey==1997]=de3$symp2[de3$Ey==1997]#1997:2,1,3
de3$Pl.2[de3$Ey==1997]=de3$symp1[de3$Ey==1997]
de3$Pl.3[de3$Ey==1997]=de3$symp3[de3$Ey==1997]
de3$Pl.1[de3$Ey==2001]=de3$symp2[de3$Ey==2001]#2001:2,1,3
de3$Pl.2[de3$Ey==2001]=de3$symp1[de3$Ey==2001]
de3$Pl.3[de3$Ey==2001]=de3$symp3[de3$Ey==2001]
de3$Pl.1[de3$Pl.1>23|de3$Pl.1<0]=NA#recoding to 0-10 scale
de3$Pl.2[de3$Pl.2>23|de3$Pl.2<0]=NA
de3$Pl.3[de3$Pl.3>23|de3$Pl.3<0]=NA
de3$Pl.1[de3$Ey==1997]=(de3$Pl.1[de3$Ey==1997]-1)
de3$Pl.1[de3$Ey==2001]=(de3$Pl.1[de3$Ey==2001]-1)
de3$Pl.2[de3$Ey==1997]=(de3$Pl.2[de3$Ey==1997]-1)
de3$Pl.2[de3$Ey==2001]=(de3$Pl.2[de3$Ey==2001]-1)
de3$Pl.3[de3$Ey==1997]=(de3$Pl.3[de3$Ey==1997]-1)
de3$Pl.3[de3$Ey==2001]=(de3$Pl.3[de3$Ey==2001]-1)
#leader likability:
de3$Ll.1=NA
de3$Ll.2=NA
de3$Ll.3=NA
de3$Ll.4=NA
de3$Ll.5=NA
de3$Ll.6=NA
de3$Ll.1[de3$Ey==1974]=de3$sympl2[de3$Ey==1974]#1974:2,1,3
de3$Ll.2[de3$Ey==1974]=de3$sympl1[de3$Ey==1974]
de3$Ll.3[de3$Ey==1974]=de3$sympl3[de3$Ey==1974]
de3$Ll.1[de3$Ey==1975]=de3$sympl2[de3$Ey==1975]#1975:2,1,3
de3$Ll.2[de3$Ey==1975]=de3$sympl1[de3$Ey==1975]
de3$Ll.3[de3$Ey==1975]=de3$sympl3[de3$Ey==1975]
de3$Ll.1[de3$Ey==1979]=de3$sympl1[de3$Ey==1979]#1979:1,2,3
de3$Ll.2[de3$Ey==1979]=de3$sympl2[de3$Ey==1979]
de3$Ll.3[de3$Ey==1979]=de3$sympl3[de3$Ey==1979]
de3$Ll.1[de3$Ey==1997]=de3$sympl2[de3$Ey==1997]#1997:2,1,3
de3$Ll.2[de3$Ey==1997]=de3$sympl1[de3$Ey==1997]
de3$Ll.3[de3$Ey==1997]=de3$sympl3[de3$Ey==1997]
de3$Ll.1[de3$Ey==2001]=de3$sympl2[de3$Ey==2001]#2001:2,1,3
de3$Ll.2[de3$Ey==2001]=de3$sympl1[de3$Ey==2001]
de3$Ll.3[de3$Ey==2001]=de3$sympl3[de3$Ey==2001]
de3$Ll.1[de3$Ll.1>25|de3$Ll.1<0]=NA#recoding to 0-10 scale
de3$Ll.2[de3$Ll.2>25|de3$Ll.2<0]=NA
de3$Ll.3[de3$Ll.3>25|de3$Ll.3<0]=NA
de3$Ll.1[de3$Ey==1997]=(de3$Ll.1[de3$Ey==1997]-1)
de3$Ll.1[de3$Ey==2001]=(de3$Ll.1[de3$Ey==2001]-1)
# de3$Ll.2[de3$Ey==1997]=(de3$Ll.2[de3$Ey==1997]-1)#already on 0-10scale
# de3$Ll.2[de3$Ey==2001]=(de3$Ll.2[de3$Ey==2001]-1)
de3$Ll.3[de3$Ey==1997]=(de3$Ll.3[de3$Ey==1997]-1)
de3$Ll.3[de3$Ey==2001]=(de3$Ll.3[de3$Ey==2001]-1)
#LRR=Left Right Respondent (voter LR self):
de3$LRR=NA#(no correspondence coding - codebook)
de3$LRR=as.numeric(de3$LRR)
#LRP.i=Left Right Parties:
de3$LRP.1=NA#(no correspondence coding - codebook)
de3$LRP.2=NA
de3$LRP.3=NA
de3$LRP.4=NA
de3$LRP.5=NA
de3$LRP.6=NA
#Elections with LRR and LRP:
de3$LR=0#(no correspondence coding - codebook)
#EeconS=Election economy state Subjective:(Retrospective socio-tropic economic evaluations)
de3$EeconS=NA#(no data)
#Elections with EeconS:
de3$Eecon=0#(no data)
#Seats to vote ratios:
de3$P1P2rat=NA
de3$P1P2rat[de3$Ey==1974]=1.05
de3$P1P2rat[de3$Ey==1975]=1.03
de3$P1P2rat[de3$Ey==1979]=1.25
de3$P1P2rat[de3$Ey==1997]=1.80
de3$P1P2rat[de3$Ey==2001]=1.93
#Gallagher Disproportionality measure:
de3$GDM=NA
de3$GDM[de3$Ey==1974]=14.96
de3$GDM[de3$Ey==1975]=15.47
de3$GDM[de3$Ey==1979]=11.58
de3$GDM[de3$Ey==1997]=16.51
de3$GDM[de3$Ey==2001]=17.76
#Party family:
de3$PartyFamilyCode1=60
de3$PartyFamilyCode2=30
de3$PartyFamilyCode3=40
de3$Pf.1=NA
de3$Pf.2=NA
de3$Pf.3=NA
de3$Pf.4=NA
de3$Pf.5=NA
de3$Pf.6=NA
de3$Pf.1[de3$Ey==1974]=de3$PartyFamilyCode2[de3$Ey==1974]#1974:2,1,3
de3$Pf.2[de3$Ey==1974]=de3$PartyFamilyCode1[de3$Ey==1974]
de3$Pf.3[de3$Ey==1974]=de3$PartyFamilyCode3[de3$Ey==1974]
de3$Pf.1[de3$Ey==1975]=de3$PartyFamilyCode2[de3$Ey==1975]#1975:2,1,3
de3$Pf.2[de3$Ey==1975]=de3$PartyFamilyCode1[de3$Ey==1975]
de3$Pf.3[de3$Ey==1975]=de3$PartyFamilyCode3[de3$Ey==1975]
de3$Pf.1[de3$Ey==1979]=de3$PartyFamilyCode1[de3$Ey==1979]#1979:1,2,3
de3$Pf.2[de3$Ey==1979]=de3$PartyFamilyCode2[de3$Ey==1979]
de3$Pf.3[de3$Ey==1979]=de3$PartyFamilyCode3[de3$Ey==1979]
de3$Pf.1[de3$Ey==1997]=de3$PartyFamilyCode2[de3$Ey==1997]#1997:2,1,3
de3$Pf.2[de3$Ey==1997]=de3$PartyFamilyCode1[de3$Ey==1997]
de3$Pf.3[de3$Ey==1997]=de3$PartyFamilyCode3[de3$Ey==1997]
de3$Pf.1[de3$Ey==2001]=de3$PartyFamilyCode2[de3$Ey==2001]#2001:2,1,3
de3$Pf.2[de3$Ey==2001]=de3$PartyFamilyCode1[de3$Ey==2001]
de3$Pf.3[de3$Ey==2001]=de3$PartyFamilyCode3[de3$Ey==2001]
#coding coalition status:
de3$CoalCode=0
#CMPid.i:
de3$CMPid.1=NA#create CMPid:
de3$CMPid.2=NA
de3$CMPid.3=NA
de3$CMPid.4=NA
de3$CMPid.5=NA
de3$CMPid.6=NA
de3$CMPid.1[de3$Ey==1974]=51320#assign CMPid by party size order for parties with likability:
de3$CMPid.2[de3$Ey==1974]=51620
de3$CMPid.3[de3$Ey==1974]=51420
de3$CMPid.1[de3$Ey==1975]=51320
de3$CMPid.2[de3$Ey==1975]=51620
de3$CMPid.3[de3$Ey==1975]=51420
de3$CMPid.1[de3$Ey==1979]=51620
de3$CMPid.2[de3$Ey==1979]=51320
de3$CMPid.3[de3$Ey==1979]=51420
de3$CMPid.1[de3$Ey==2001]=51320
de3$CMPid.2[de3$Ey==2001]=51620
de3$CMPid.3[de3$Ey==2001]=51421
de3$CMPid.1=as.numeric(de3$CMPid.1)#recode as numeric:
de3$CMPid.2=as.numeric(de3$CMPid.2)
de3$CMPid.3=as.numeric(de3$CMPid.3)
de3$CMPid.4=as.numeric(de3$CMPid.4)
de3$CMPid.5=as.numeric(de3$CMPid.5)
de3$CMPid.6=as.numeric(de3$CMPid.6)
#PrePost:
de3$PrePost=as.numeric(NA)
de3$PrePost[de3$Ey==1974]=1
de3$PrePost[de3$Ey==1975]=1
de3$PrePost[de3$Ey==1979]=1
#"Pi"#Party identification:
de3$Pi=NA
de3$Pi[de3$Ey==1974&de3$pid==2]=1#1974:2,1,3
de3$Pi[de3$Ey==1974&de3$pid==1]=2
de3$Pi[de3$Ey==1974&de3$pid==3]=3
de3$Pi[de3$Ey==1975&de3$pid==2]=1#1975:2,1,3
de3$Pi[de3$Ey==1975&de3$pid==1]=2
de3$Pi[de3$Ey==1975&de3$pid==3]=3
de3$Pi[de3$Ey==1979&de3$pid==1]=1#1979:1,2,3
de3$Pi[de3$Ey==1979&de3$pid==2]=2
de3$Pi[de3$Ey==1979&de3$pid==3]=3
de3$Pi[de3$Ey==1997&de3$pid==2]=1#1997:2,1,3
de3$Pi[de3$Ey==1997&de3$pid==1]=2
de3$Pi[de3$Ey==1997&de3$pid==3]=3
de3$Pi[de3$Ey==2001&de3$pid==2]=1#2001:2,1,3
de3$Pi[de3$Ey==2001&de3$pid==1]=2
de3$Pi[de3$Ey==2001&de3$pid==3]=3
#subset:
colnames(de3)
de3=de3[,c(1,2,3,145,49,144,146:157,165,158,159:164,167,166,168,169,173:186,8,187)]
colnames(de3)
de3[colnames(de3)]=sapply(de3[colnames(de3)],as.numeric)


#NETHERLANDS EV de4:----
de4=read_dta("ZA3911_nl_1971-1998.dta")
colnames(de4)
colnames(de4)[colnames(de4)=="year"]="Ey"#Election Year
de4$Ey=as.numeric(de4$Ey)
de4=de4[de4$Ey %in% c(1986,1994,1998),]#get rid of elections without likability
colnames(de4)[colnames(de4)=="country"]="Ec"#Election Country
de4$Ec=as.numeric(de4$Ec)
colnames(de4)[colnames(de4)=="age"]="Rage"#Respondent Age
de4$Rage=as.numeric(de4$Rage)
de4$Rage[de4$Rage>100]=NA
#Vt#Vote Turnout (0 did not vote; 1 voted)
colnames(de4)[colnames(de4)=="turnout"]="Vt"
de4$Vt[de4$Vt>2]=NA
#"Va"#Vote Alphabetical (here numbers), by vote share:
de4$Va=NA
de4$Va[de4$Ey==1986&de4$partycho==5]=1#1986:5,2,8,3
de4$Va[de4$Ey==1986&de4$partycho==2]=2
de4$Va[de4$Ey==1986&de4$partycho==8]=3
de4$Va[de4$Ey==1986&de4$partycho==3]=4
de4$Va[de4$Ey==1994&de4$partycho==2]=1#1994:2,5,8,3
de4$Va[de4$Ey==1994&de4$partycho==5]=2
de4$Va[de4$Ey==1994&de4$partycho==8]=3
de4$Va[de4$Ey==1994&de4$partycho==3]=4
de4$Va[de4$Ey==1998&de4$partycho==2]=1#1998:2,8,5,3
de4$Va[de4$Ey==1998&de4$partycho==8]=2
de4$Va[de4$Ey==1998&de4$partycho==5]=3
de4$Va[de4$Ey==1998&de4$partycho==3]=4
colnames(de4)[colnames(de4)=="weight2"]="Rwd"##Respondent Weight Demographic
de4$Rwd=as.numeric(de4$Rwd)
de4$Rwd[de4$Rwd==0]=1#missing weight put to neutral weight of 1
#Number of Parties:
de4$NP=NA
de4$NP[de4$Ey==1986]=4
de4$NP[de4$Ey==1994]=4
de4$NP[de4$Ey==1998]=4
#party likability:
de4$Pl.1=NA
de4$Pl.2=NA
de4$Pl.3=NA
de4$Pl.4=NA
de4$Pl.5=NA
de4$Pl.6=NA
# de4$Pl.7=NA
de4$Pl.1[de4$Ey==1986]=de4$symp5[de4$Ey==1986]#1986:5,2,8,3
de4$Pl.2[de4$Ey==1986]=de4$symp2[de4$Ey==1986]
de4$Pl.3[de4$Ey==1986]=de4$symp8[de4$Ey==1986]
de4$Pl.4[de4$Ey==1986]=de4$symp3[de4$Ey==1986]
de4$Pl.1[de4$Ey==1994]=de4$symp2[de4$Ey==1994]#1994:2,5,8,3
de4$Pl.2[de4$Ey==1994]=de4$symp5[de4$Ey==1994]
de4$Pl.3[de4$Ey==1994]=de4$symp8[de4$Ey==1994]
de4$Pl.4[de4$Ey==1994]=de4$symp3[de4$Ey==1994]
de4$Pl.1[de4$Ey==1998]=de4$symp2[de4$Ey==1998]#1998:2,8,5,3
de4$Pl.2[de4$Ey==1998]=de4$symp8[de4$Ey==1998]
de4$Pl.3[de4$Ey==1998]=de4$symp5[de4$Ey==1998]
de4$Pl.4[de4$Ey==1998]=de4$symp3[de4$Ey==1998]
de4$Pl.1[de4$Pl.1>100]=NA#recoding to 0-10 scale
de4$Pl.2[de4$Pl.2>100]=NA
de4$Pl.3[de4$Pl.3>100]=NA
de4$Pl.4[de4$Pl.4>100]=NA
de4$Pl.1=de4$Pl.1/10
de4$Pl.2=de4$Pl.2/10
de4$Pl.3=de4$Pl.3/10
de4$Pl.4=de4$Pl.4/10
#leader likability:
de4$Ll.1=NA
de4$Ll.2=NA
de4$Ll.3=NA
de4$Ll.4=NA
de4$Ll.5=NA
de4$Ll.6=NA
# de4$Ll.7=NA
de4$Ll.1[de4$Ey==1986]=de4$sympl5[de4$Ey==1986]#1986:5,2,8,3
de4$Ll.2[de4$Ey==1986]=de4$sympl2[de4$Ey==1986]
de4$Ll.3[de4$Ey==1986]=de4$sympl8[de4$Ey==1986]
de4$Ll.4[de4$Ey==1986]=de4$sympl3[de4$Ey==1986]
de4$Ll.1[de4$Ey==1994]=de4$sympl2[de4$Ey==1994]#1994:2,5,8,3
de4$Ll.2[de4$Ey==1994]=de4$sympl5[de4$Ey==1994]
de4$Ll.3[de4$Ey==1994]=de4$sympl8[de4$Ey==1994]
de4$Ll.4[de4$Ey==1994]=de4$sympl3[de4$Ey==1994]
de4$Ll.1[de4$Ey==1998]=de4$sympl2[de4$Ey==1998]#1998:2,8,5,3
de4$Ll.2[de4$Ey==1998]=de4$sympl8[de4$Ey==1998]
de4$Ll.3[de4$Ey==1998]=de4$sympl5[de4$Ey==1998]
de4$Ll.4[de4$Ey==1998]=de4$sympl3[de4$Ey==1998]
de4$Ll.1[de4$Ll.1>100]=NA#recoding to 0-10 scale
de4$Ll.2[de4$Ll.2>100]=NA
de4$Ll.3[de4$Ll.3>100]=NA
de4$Ll.4[de4$Ll.4>100]=NA
de4$Ll.1=de4$Ll.1/10
de4$Ll.2=de4$Ll.2/10
de4$Ll.3=de4$Ll.3/10
de4$Ll.4=de4$Ll.4/10
#LRR=Left Right Respondent (voter LR self):
colnames(de4)[colnames(de4)=="lrself"]="LRR"
de4$LRR=(de4$LRR-1)*10/9#recoding to 0-10 scale
de4$LRR[de4$LRR>10]=NA
de4$LRR=as.numeric(de4$LRR)
#LRP.i=Left Right Parties:
de4$LRP.1=NA
de4$LRP.2=NA
de4$LRP.3=NA
de4$LRP.4=NA
de4$LRP.5=NA
de4$LRP.6=NA
# de4$LRP.7=NA
de4$LRP.1[de4$Ey==1986]=de4$lrp5[de4$Ey==1986]#1986:5,2,8,3
de4$LRP.2[de4$Ey==1986]=de4$lrp2[de4$Ey==1986]
de4$LRP.3[de4$Ey==1986]=de4$lrp8[de4$Ey==1986]
de4$LRP.4[de4$Ey==1986]=de4$lrp3[de4$Ey==1986]
de4$LRP.1[de4$Ey==1994]=de4$lrp2[de4$Ey==1994]#1994:2,5,8,3
de4$LRP.2[de4$Ey==1994]=de4$lrp5[de4$Ey==1994]
de4$LRP.3[de4$Ey==1994]=de4$lrp8[de4$Ey==1994]
de4$LRP.4[de4$Ey==1994]=de4$lrp3[de4$Ey==1994]
de4$LRP.1[de4$Ey==1998]=de4$lrp2[de4$Ey==1998]#1998:2,8,5,3
de4$LRP.2[de4$Ey==1998]=de4$lrp8[de4$Ey==1998]
de4$LRP.3[de4$Ey==1998]=de4$lrp5[de4$Ey==1998]
de4$LRP.4[de4$Ey==1998]=de4$lrp3[de4$Ey==1998]
de4$LRP.1=(de4$LRP.1-1)*10/9#recoding to 0-10 scale
de4$LRP.1[de4$LRP.1>10]=NA
de4$LRP.2=(de4$LRP.2-1)*10/9
de4$LRP.2[de4$LRP.2>10]=NA
de4$LRP.3=(de4$LRP.3-1)*10/9
de4$LRP.3[de4$LRP.3>10]=NA
de4$LRP.4=(de4$LRP.4-1)*10/9
de4$LRP.4[de4$LRP.4>10]=NA
de4$LRP.5=(de4$LRP.5-1)*10/9
de4$LRP.5[de4$LRP.5>10]=NA
de4$LRP.6=(de4$LRP.6-1)*10/9
de4$LRP.6[de4$LRP.6>10]=NA
#Elections with LRR and LRP:
de4$LR=NA
de4$LR[de4$Ey==1986]=1
de4$LR[de4$Ey==1994]=1
de4$LR[de4$Ey==1998]=1
#EeconS=Election economy state Subjective:(Retrospective socio-tropic economic evaluations)
colnames(de4)[colnames(de4)=="economy1"]="EeconS"
de4$EeconS=NA#(no data)
#Elections with EeconS:
de4$Eecon=0#(no data)
#Seats to vote ratios:
de4$P1P2rat=NA
de4$P1P2rat[de4$Ey==1986]=1.00
de4$P1P2rat[de4$Ey==1994]=1.01
de4$P1P2rat[de4$Ey==1998]=1.01
#Gallagher Disproportionality measure:
de4$GDM=NA
de4$GDM[de4$Ey==1986]=1.67
de4$GDM[de4$Ey==1994]=1.08
de4$GDM[de4$Ey==1998]=1.28
#Party family:
de4$PartyFamilyCode2=30
de4$PartyFamilyCode3=40
de4$PartyFamilyCode5=50
de4$PartyFamilyCode8=40
de4$Pf.1=NA
de4$Pf.2=NA
de4$Pf.3=NA
de4$Pf.4=NA
de4$Pf.5=NA
de4$Pf.6=NA
# de4$Pf.7=NA
de4$Pf.1[de4$Ey==1986]=de4$PartyFamilyCode5[de4$Ey==1986]#1986:5,2,8,3
de4$Pf.2[de4$Ey==1986]=de4$PartyFamilyCode2[de4$Ey==1986]
de4$Pf.3[de4$Ey==1986]=de4$PartyFamilyCode8[de4$Ey==1986]
de4$Pf.4[de4$Ey==1986]=de4$PartyFamilyCode3[de4$Ey==1986]
de4$Pf.1[de4$Ey==1994]=de4$PartyFamilyCode2[de4$Ey==1994]#1994:2,5,8,3
de4$Pf.2[de4$Ey==1994]=de4$PartyFamilyCode5[de4$Ey==1994]
de4$Pf.3[de4$Ey==1994]=de4$PartyFamilyCode8[de4$Ey==1994]
de4$Pf.4[de4$Ey==1994]=de4$PartyFamilyCode3[de4$Ey==1994]
de4$Pf.1[de4$Ey==1998]=de4$PartyFamilyCode2[de4$Ey==1998]#1998:2,8,5,3
de4$Pf.2[de4$Ey==1998]=de4$PartyFamilyCode8[de4$Ey==1998]
de4$Pf.3[de4$Ey==1998]=de4$PartyFamilyCode5[de4$Ey==1998]
de4$Pf.4[de4$Ey==1998]=de4$PartyFamilyCode3[de4$Ey==1998]
#coding coalition status:
de4$CoalCode=0
#CMPid.i:
de4$CMPid.1=NA#create CMPid:
de4$CMPid.2=NA
de4$CMPid.3=NA
de4$CMPid.4=NA
de4$CMPid.5=NA
de4$CMPid.6=NA
de4$CMPid.1[de4$Ey==1986]=22521#assign CMPid by party size order for parties with likability:
de4$CMPid.2[de4$Ey==1986]=22320
de4$CMPid.3[de4$Ey==1986]=22420
de4$CMPid.4[de4$Ey==1986]=22330
de4$CMPid.1=as.numeric(de4$CMPid.1)#recode as numeric:
de4$CMPid.2=as.numeric(de4$CMPid.2)
de4$CMPid.3=as.numeric(de4$CMPid.3)
de4$CMPid.4=as.numeric(de4$CMPid.4)
de4$CMPid.5=as.numeric(de4$CMPid.5)
de4$CMPid.6=as.numeric(de4$CMPid.6)
#PrePost:
de4$PrePost=as.numeric(NA)
de4$PrePost[de4$Ey==1986]=3
#"Pi"#Party identification:
de4$Pi=NA
de4$Pi[de4$Ey==1986&de4$pid==5]=1#1986:5,2,8,3
de4$Pi[de4$Ey==1986&de4$pid==2]=2
de4$Pi[de4$Ey==1986&de4$pid==8]=3
de4$Pi[de4$Ey==1986&de4$pid==3]=4
de4$Pi[de4$Ey==1994&de4$pid==2]=1#1994:2,5,8,3
de4$Pi[de4$Ey==1994&de4$pid==5]=2
de4$Pi[de4$Ey==1994&de4$pid==8]=3
de4$Pi[de4$Ey==1994&de4$pid==3]=4
de4$Pi[de4$Ey==1998&de4$pid==2]=1#1998:2,8,5,3
de4$Pi[de4$Ey==1998&de4$pid==8]=2
de4$Pi[de4$Ey==1998&de4$pid==5]=3
de4$Pi[de4$Ey==1998&de4$pid==3]=4
#subset:
colnames(de4)
de4=de4[,c(1,2,4,98,91,97,99:110,117,53,111:116,119,118,120,121,126:139,9,140)]
colnames(de4)
de4[colnames(de4)]=sapply(de4[colnames(de4)],as.numeric)


#NORWAY EV de5:----
de5=read_dta("ZA3911_no_1965-1997.dta")
colnames(de5)
colnames(de5)[colnames(de5)=="year"]="Ey"#Election Year
de5$Ey=as.numeric(de5$Ey)
de5=de5[de5$Ey>=1981,]#get rid of elections without likability
colnames(de5)[colnames(de5)=="country"]="Ec"#Election Country
de5$Ec=as.numeric(de5$Ec)
colnames(de5)[colnames(de5)=="age"]="Rage"#Respondent Age
de5$Rage=as.numeric(de5$Rage)
de5$Rage[de5$Rage>100]=NA
#Vt#Vote Turnout (0 did not vote; 1 voted)
colnames(de5)[colnames(de5)=="turnout"]="Vt"
de5$Vt[de5$Vt>2]=NA
de5$Vt[de5$Vt==2]=0
#"Va"#Vote Alphabetical (here numbers), by vote share:
de5$Va=NA
de5$Va[de5$Ey==1981&de5$choice==3]=1#1981:3,8,5,6,2,9
de5$Va[de5$Ey==1981&de5$choice==8]=2
de5$Va[de5$Ey==1981&de5$choice==5]=3
de5$Va[de5$Ey==1981&de5$choice==6]=4
de5$Va[de5$Ey==1981&de5$choice==2]=5
de5$Va[de5$Ey==1981&de5$choice==9]=6
de5$Va[de5$Ey==1985&de5$choice==3]=1#1985:3,8,5,6,2,9
de5$Va[de5$Ey==1985&de5$choice==8]=2
de5$Va[de5$Ey==1985&de5$choice==5]=3
de5$Va[de5$Ey==1985&de5$choice==6]=4
de5$Va[de5$Ey==1985&de5$choice==2]=5
de5$Va[de5$Ey==1985&de5$choice==9]=6
de5$Va[de5$Ey==1989&de5$choice==3]=1#1989:3,8,9,2,5,6
de5$Va[de5$Ey==1989&de5$choice==8]=2
de5$Va[de5$Ey==1989&de5$choice==9]=3
de5$Va[de5$Ey==1989&de5$choice==2]=4
de5$Va[de5$Ey==1989&de5$choice==5]=5
de5$Va[de5$Ey==1989&de5$choice==6]=6
de5$Va[de5$Ey==1993&de5$choice==3]=1#1993:3,6,8,2,5,9
de5$Va[de5$Ey==1993&de5$choice==6]=2
de5$Va[de5$Ey==1993&de5$choice==8]=3
de5$Va[de5$Ey==1993&de5$choice==2]=4
de5$Va[de5$Ey==1993&de5$choice==5]=5
de5$Va[de5$Ey==1993&de5$choice==9]=6
de5$Va[de5$Ey==1997&de5$choice==3]=1#1997:3,9,5,8,6,2
de5$Va[de5$Ey==1997&de5$choice==9]=2
de5$Va[de5$Ey==1997&de5$choice==5]=3
de5$Va[de5$Ey==1997&de5$choice==8]=4
de5$Va[de5$Ey==1997&de5$choice==6]=5
de5$Va[de5$Ey==1997&de5$choice==2]=6
colnames(de5)[colnames(de5)=="weight"]="Rwd"#Respondent Weight Demographic (there's no weight, all entries are 1)
de5$Rwd=as.numeric(de5$Rwd)#tehy are all 1, there is no information on weights
summary(de5$Rwd)
#Number of Parties:
de5$NP=NA
de5$NP[de5$Ey==1981]=6
de5$NP[de5$Ey==1985]=6
de5$NP[de5$Ey==1989]=6
de5$NP[de5$Ey==1993]=6
de5$NP[de5$Ey==1997]=6
#party likability:
de5$Pl.1=NA
de5$Pl.2=NA
de5$Pl.3=NA
de5$Pl.4=NA
de5$Pl.5=NA
de5$Pl.6=NA
# de5$Pl.7=NA
de5$Pl.1[de5$Ey==1981]=de5$symplab[de5$Ey==1981]#1981:3,8,5,6,2,9
de5$Pl.2[de5$Ey==1981]=de5$sympcons[de5$Ey==1981]
de5$Pl.3[de5$Ey==1981]=de5$sympchr[de5$Ey==1981]
de5$Pl.4[de5$Ey==1981]=de5$sympc[de5$Ey==1981]
de5$Pl.5[de5$Ey==1981]=de5$sympsoc[de5$Ey==1981]
de5$Pl.6[de5$Ey==1981]=de5$sympprog[de5$Ey==1981]
de5$Pl.1[de5$Ey==1985]=de5$symplab[de5$Ey==1985]#1985:3,8,5,6,2,9
de5$Pl.2[de5$Ey==1985]=de5$sympcons[de5$Ey==1985]
de5$Pl.3[de5$Ey==1985]=de5$sympchr[de5$Ey==1985]
de5$Pl.4[de5$Ey==1985]=de5$sympc[de5$Ey==1985]
de5$Pl.5[de5$Ey==1985]=de5$sympsoc[de5$Ey==1985]
de5$Pl.6[de5$Ey==1985]=de5$sympprog[de5$Ey==1985]
de5$Pl.1[de5$Ey==1989]=de5$symplab[de5$Ey==1989]#1989:3,8,9,2,5,6
de5$Pl.2[de5$Ey==1989]=de5$sympcons[de5$Ey==1989]
de5$Pl.3[de5$Ey==1989]=de5$sympprog[de5$Ey==1989]
de5$Pl.4[de5$Ey==1989]=de5$sympsoc[de5$Ey==1989]
de5$Pl.5[de5$Ey==1989]=de5$sympchr[de5$Ey==1989]
de5$Pl.6[de5$Ey==1989]=de5$sympc[de5$Ey==1989]
de5$Pl.1[de5$Ey==1993]=de5$symplab[de5$Ey==1993]#1993:3,6,8,2,5,9
de5$Pl.2[de5$Ey==1993]=de5$sympc[de5$Ey==1993]
de5$Pl.3[de5$Ey==1993]=de5$sympcons[de5$Ey==1993]
de5$Pl.4[de5$Ey==1993]=de5$sympsoc[de5$Ey==1993]
de5$Pl.5[de5$Ey==1993]=de5$sympchr[de5$Ey==1993]
de5$Pl.6[de5$Ey==1993]=de5$sympprog[de5$Ey==1993]
de5$Pl.1[de5$Ey==1997]=de5$symplab[de5$Ey==1997]#1997:3,9,5,8,6,2
de5$Pl.2[de5$Ey==1997]=de5$sympprog[de5$Ey==1997]
de5$Pl.3[de5$Ey==1997]=de5$sympchr[de5$Ey==1997]
de5$Pl.4[de5$Ey==1997]=de5$sympcons[de5$Ey==1997]
de5$Pl.5[de5$Ey==1997]=de5$sympc[de5$Ey==1997]
de5$Pl.6[de5$Ey==1997]=de5$sympsoc[de5$Ey==1997]
de5$Pl.1=de5$Pl.1/10#recoding to 0-10 scale
de5$Pl.2=de5$Pl.2/10
de5$Pl.3=de5$Pl.3/10
de5$Pl.4=de5$Pl.4/10
de5$Pl.5=de5$Pl.5/10
de5$Pl.6=de5$Pl.6/10
#leader likability:
de5$Ll.1=NA
de5$Ll.2=NA
de5$Ll.3=NA
de5$Ll.4=NA
de5$Ll.5=NA
de5$Ll.6=NA
# de5$Ll.7=NA
de5$Ll.1[de5$Ey==1981]=de5$symplabl[de5$Ey==1981]#1981:3,8,5,6,2,9
de5$Ll.2[de5$Ey==1981]=de5$sympconl[de5$Ey==1981]
de5$Ll.3[de5$Ey==1981]=de5$sympchrl[de5$Ey==1981]
de5$Ll.4[de5$Ey==1981]=de5$sympcl[de5$Ey==1981]
de5$Ll.5[de5$Ey==1981]=de5$sympsocl[de5$Ey==1981]
de5$Ll.6[de5$Ey==1981]=de5$sympprol[de5$Ey==1981]
de5$Ll.1[de5$Ey==1985]=de5$symplabl[de5$Ey==1985]#1985:3,8,5,6,2,9
de5$Ll.2[de5$Ey==1985]=de5$sympconl[de5$Ey==1985]
de5$Ll.3[de5$Ey==1985]=de5$sympchrl[de5$Ey==1985]
de5$Ll.4[de5$Ey==1985]=de5$sympcl[de5$Ey==1985]
de5$Ll.5[de5$Ey==1985]=de5$sympsocl[de5$Ey==1985]
de5$Ll.6[de5$Ey==1985]=de5$sympprol[de5$Ey==1985]
de5$Ll.1[de5$Ey==1989]=de5$symplabl[de5$Ey==1989]#1989:3,8,9,2,5,6
de5$Ll.2[de5$Ey==1989]=de5$sympconl[de5$Ey==1989]
de5$Ll.3[de5$Ey==1989]=de5$sympprol[de5$Ey==1989]
de5$Ll.4[de5$Ey==1989]=de5$sympsocl[de5$Ey==1989]
de5$Ll.5[de5$Ey==1989]=de5$sympchrl[de5$Ey==1989]
de5$Ll.6[de5$Ey==1989]=de5$sympcl[de5$Ey==1989]
de5$Ll.1[de5$Ey==1993]=de5$symplabl[de5$Ey==1993]#1993:3,6,8,2,5,9
de5$Ll.2[de5$Ey==1993]=de5$sympcl[de5$Ey==1993]
de5$Ll.3[de5$Ey==1993]=de5$sympconl[de5$Ey==1993]
de5$Ll.4[de5$Ey==1993]=de5$sympsocl[de5$Ey==1993]
de5$Ll.5[de5$Ey==1993]=de5$sympchrl[de5$Ey==1993]
de5$Ll.6[de5$Ey==1993]=de5$sympprol[de5$Ey==1993]
de5$Ll.1[de5$Ey==1997]=de5$symplabl[de5$Ey==1997]#1997:3,9,5,8,6,2
de5$Ll.2[de5$Ey==1997]=de5$sympprol[de5$Ey==1997]
de5$Ll.3[de5$Ey==1997]=de5$sympchrl[de5$Ey==1997]
de5$Ll.4[de5$Ey==1997]=de5$sympconl[de5$Ey==1997]
de5$Ll.5[de5$Ey==1997]=de5$sympcl[de5$Ey==1997]
de5$Ll.6[de5$Ey==1997]=de5$sympsocl[de5$Ey==1997]
de5$Ll.1=de5$Ll.1/10#recoding to 0-10 scale
de5$Ll.2=de5$Ll.2/10
de5$Ll.3=de5$Ll.3/10
de5$Ll.4=de5$Ll.4/10
de5$Ll.5=de5$Ll.5/10
de5$Ll.6=de5$Ll.6/10
#LRR=Left Right Respondent (voter LR self):
colnames(de5)[colnames(de5)=="lrsp"]="LRR"
de5$LRR=as.numeric(de5$LRR)
#recoding to 0-10 scale: already 0-10 scale
#LRP.i=Left Right Parties:
de5$LRP.1=NA
de5$LRP.2=NA
de5$LRP.3=NA
de5$LRP.4=NA
de5$LRP.5=NA
de5$LRP.6=NA
# de5$LRP.7=NA
de5$LRP.1[de5$Ey==1981]=de5$lrlab[de5$Ey==1981]#1981:3,8,5,6,2,9
de5$LRP.2[de5$Ey==1981]=de5$lrcons[de5$Ey==1981]
de5$LRP.3[de5$Ey==1981]=de5$lrchr[de5$Ey==1981]
de5$LRP.4[de5$Ey==1981]=de5$lrc[de5$Ey==1981]
de5$LRP.5[de5$Ey==1981]=de5$lrsoc[de5$Ey==1981]
de5$LRP.6[de5$Ey==1981]=de5$lrprog[de5$Ey==1981]
de5$LRP.1[de5$Ey==1985]=de5$lrlab[de5$Ey==1985]#1985:3,8,5,6,2,9
de5$LRP.2[de5$Ey==1985]=de5$lrcons[de5$Ey==1985]
de5$LRP.3[de5$Ey==1985]=de5$lrchr[de5$Ey==1985]
de5$LRP.4[de5$Ey==1985]=de5$lrc[de5$Ey==1985]
de5$LRP.5[de5$Ey==1985]=de5$lrsoc[de5$Ey==1985]
de5$LRP.6[de5$Ey==1985]=de5$lrprog[de5$Ey==1985]
de5$LRP.1[de5$Ey==1989]=de5$lrlab[de5$Ey==1989]#1989:3,8,9,2,5,6
de5$LRP.2[de5$Ey==1989]=de5$lrcons[de5$Ey==1989]
de5$LRP.3[de5$Ey==1989]=de5$lrprog[de5$Ey==1989]
de5$LRP.4[de5$Ey==1989]=de5$lrsoc[de5$Ey==1989]
de5$LRP.5[de5$Ey==1989]=de5$lrchr[de5$Ey==1989]
de5$LRP.6[de5$Ey==1989]=de5$lrc[de5$Ey==1989]
de5$LRP.1[de5$Ey==1993]=de5$lrlab[de5$Ey==1993]#1993:3,6,8,2,5,9
de5$LRP.2[de5$Ey==1993]=de5$lrc[de5$Ey==1993]
de5$LRP.3[de5$Ey==1993]=de5$lrcons[de5$Ey==1993]
de5$LRP.4[de5$Ey==1993]=de5$lrsoc[de5$Ey==1993]
de5$LRP.5[de5$Ey==1993]=de5$lrchr[de5$Ey==1993]
de5$LRP.6[de5$Ey==1993]=de5$lrprog[de5$Ey==1993]
de5$LRP.1[de5$Ey==1997]=de5$lrlab[de5$Ey==1997]#1997:3,9,5,8,6,2
de5$LRP.2[de5$Ey==1997]=de5$lrprog[de5$Ey==1997]
de5$LRP.3[de5$Ey==1997]=de5$lrchr[de5$Ey==1997]
de5$LRP.4[de5$Ey==1997]=de5$lrcons[de5$Ey==1997]
de5$LRP.5[de5$Ey==1997]=de5$lrc[de5$Ey==1997]
de5$LRP.6[de5$Ey==1997]=de5$lrsoc[de5$Ey==1997]
#Elections with LRR and LRP:
de5$LR=NA
de5$LR[de5$Ey==1981]=1
de5$LR[de5$Ey==1985]=1
de5$LR[de5$Ey==1989]=1
de5$LR[de5$Ey==1993]=1
de5$LR[de5$Ey==1997]=1
#recoding to 0-10 scale: already 0-10 scale
#EeconS=Election economy state Subjective:(Retrospective socio-tropic economic evaluations)
# 1. MUCH BETTER                 
# 2. SOMEWHAT BETTER     
# 3. STAYED THE SAME              
# 4. SOMEWHAT WORSE
# 5. MUCH WORSE
colnames(de5)[colnames(de5)=="econom3"]="EeconS"
#recode EeconS from EV scale
# 1. GOTTEN BETTER c(1,2,3,4,5)
# 5. GOTTEN WORSE 
# to TEV scale, noting that the TEV used the conversion scale below for same elections:
# 0. Negative evaluation c(0, 0.25, 0.5, 0.75, 1)
# 1. Positive evaluation
de5$EeconS[de5$EeconS==1]=1
de5$EeconS[de5$EeconS==2]=0.75
de5$EeconS[de5$EeconS==3]=0.5
de5$EeconS[de5$EeconS==4]=0.25
de5$EeconS[de5$EeconS==5]=0
#Elections with EeconS:
de5$Eecon=NA
de5$Eecon[de5$Ey==1981]=0
de5$Eecon[de5$Ey==1985]=1
de5$Eecon[de5$Ey==1989]=1
de5$Eecon[de5$Ey==1993]=1
de5$Eecon[de5$Ey==1997]=1
#Seats to vote ratios:
de5$P1P2rat=NA
de5$P1P2rat[de5$Ey==1981]=1.06
de5$P1P2rat[de5$Ey==1985]=1.06
de5$P1P2rat[de5$Ey==1989]=1.10
de5$P1P2rat[de5$Ey==1993]=0.95
de5$P1P2rat[de5$Ey==1997]=1.14
#Gallagher Disproportionality measure:
de5$GDM=NA
de5$GDM[de5$Ey==1981]=4.94
de5$GDM[de5$Ey==1985]=4.75
de5$GDM[de5$Ey==1989]=3.67
de5$GDM[de5$Ey==1993]=3.95
de5$GDM[de5$Ey==1997]=3.44
#Party family:
de5$PartyFamilyCode2=20#Sosialistisk Folkeparti (Socialist People’s Party)
de5$PartyFamilyCode3=30#Det norske Arbeiderparti (Norwegian Labour Party)
de5$PartyFamilyCode5=50#Kristelig Folkeparti (Christian People’s Party)
de5$PartyFamilyCode6=80#Senterpartiet (Centre Party)
de5$PartyFamilyCode8=60#Høyre (Conservative Party)
de5$PartyFamilyCode9=70#Fremskrittspartiet (Progress Party)
# 0            rv
# 1           nkp
# 2            sv
# 3           dna
# 4             v
# 5           Krf
# 6            Sp
# 7           dlf
# 8             h
# 9      Frp(ALP)
de5$Pf.1=NA
de5$Pf.2=NA
de5$Pf.3=NA
de5$Pf.4=NA
de5$Pf.5=NA
de5$Pf.6=NA
# de5$Pf.7=NA
de5$Pf.1[de5$Ey==1981]=de5$PartyFamilyCode3[de5$Ey==1981]#1981:3,8,5,6,2,9
de5$Pf.2[de5$Ey==1981]=de5$PartyFamilyCode8[de5$Ey==1981]
de5$Pf.3[de5$Ey==1981]=de5$PartyFamilyCode5[de5$Ey==1981]
de5$Pf.4[de5$Ey==1981]=de5$PartyFamilyCode6[de5$Ey==1981]
de5$Pf.5[de5$Ey==1981]=de5$PartyFamilyCode2[de5$Ey==1981]
de5$Pf.6[de5$Ey==1981]=de5$PartyFamilyCode9[de5$Ey==1981]
de5$Pf.1[de5$Ey==1985]=de5$PartyFamilyCode3[de5$Ey==1985]#1985:3,8,5,6,2,9
de5$Pf.2[de5$Ey==1985]=de5$PartyFamilyCode8[de5$Ey==1985]
de5$Pf.3[de5$Ey==1985]=de5$PartyFamilyCode5[de5$Ey==1985]
de5$Pf.4[de5$Ey==1985]=de5$PartyFamilyCode6[de5$Ey==1985]
de5$Pf.5[de5$Ey==1985]=de5$PartyFamilyCode2[de5$Ey==1985]
de5$Pf.6[de5$Ey==1985]=de5$PartyFamilyCode9[de5$Ey==1985]
de5$Pf.1[de5$Ey==1989]=de5$PartyFamilyCode3[de5$Ey==1989]#1989:3,8,9,2,5,6
de5$Pf.2[de5$Ey==1989]=de5$PartyFamilyCode8[de5$Ey==1989]
de5$Pf.3[de5$Ey==1989]=de5$PartyFamilyCode9[de5$Ey==1989]
de5$Pf.4[de5$Ey==1989]=de5$PartyFamilyCode2[de5$Ey==1989]
de5$Pf.5[de5$Ey==1989]=de5$PartyFamilyCode5[de5$Ey==1989]
de5$Pf.6[de5$Ey==1989]=de5$PartyFamilyCode6[de5$Ey==1989]
de5$Pf.1[de5$Ey==1993]=de5$PartyFamilyCode3[de5$Ey==1993]#1993:3,6,8,2,5,9
de5$Pf.2[de5$Ey==1993]=de5$PartyFamilyCode6[de5$Ey==1993]
de5$Pf.3[de5$Ey==1993]=de5$PartyFamilyCode8[de5$Ey==1993]
de5$Pf.4[de5$Ey==1993]=de5$PartyFamilyCode2[de5$Ey==1993]
de5$Pf.5[de5$Ey==1993]=de5$PartyFamilyCode5[de5$Ey==1993]
de5$Pf.6[de5$Ey==1993]=de5$PartyFamilyCode9[de5$Ey==1993]
de5$Pf.1[de5$Ey==1997]=de5$PartyFamilyCode3[de5$Ey==1997]#1997:3,9,5,8,6,2
de5$Pf.2[de5$Ey==1997]=de5$PartyFamilyCode9[de5$Ey==1997]
de5$Pf.3[de5$Ey==1997]=de5$PartyFamilyCode5[de5$Ey==1997]
de5$Pf.4[de5$Ey==1997]=de5$PartyFamilyCode8[de5$Ey==1997]
de5$Pf.5[de5$Ey==1997]=de5$PartyFamilyCode6[de5$Ey==1997]
de5$Pf.6[de5$Ey==1997]=de5$PartyFamilyCode2[de5$Ey==1997]
#coding coalition status:
de5$CoalCode=0
#CMPid.i:
de5$CMPid.1=NA#create CMPid:
de5$CMPid.2=NA
de5$CMPid.3=NA
de5$CMPid.4=NA
de5$CMPid.5=NA
de5$CMPid.6=NA
de5$CMPid.1=as.numeric(de5$CMPid.1)#recode as numeric:
de5$CMPid.2=as.numeric(de5$CMPid.2)
de5$CMPid.3=as.numeric(de5$CMPid.3)
de5$CMPid.4=as.numeric(de5$CMPid.4)
de5$CMPid.5=as.numeric(de5$CMPid.5)
de5$CMPid.6=as.numeric(de5$CMPid.6)
#PrePost:
de5$PrePost=as.numeric(NA)
#"Pi"#Party identification:
de5$Pi=NA
de5$Pi[de5$Ey==1981&de5$pid==3]=1#1981:3,8,5,6,2,9
de5$Pi[de5$Ey==1981&de5$pid==8]=2
de5$Pi[de5$Ey==1981&de5$pid==5]=3
de5$Pi[de5$Ey==1981&de5$pid==6]=4
de5$Pi[de5$Ey==1981&de5$pid==2]=5
de5$Pi[de5$Ey==1981&de5$pid==9]=6
de5$Pi[de5$Ey==1985&de5$pid==3]=1#1985:3,8,5,6,2,9
de5$Pi[de5$Ey==1985&de5$pid==8]=2
de5$Pi[de5$Ey==1985&de5$pid==5]=3
de5$Pi[de5$Ey==1985&de5$pid==6]=4
de5$Pi[de5$Ey==1985&de5$pid==2]=5
de5$Pi[de5$Ey==1985&de5$pid==9]=6
de5$Pi[de5$Ey==1989&de5$pid==3]=1#1989:3,8,9,2,5,6
de5$Pi[de5$Ey==1989&de5$pid==8]=2
de5$Pi[de5$Ey==1989&de5$pid==9]=3
de5$Pi[de5$Ey==1989&de5$pid==2]=4
de5$Pi[de5$Ey==1989&de5$pid==5]=5
de5$Pi[de5$Ey==1989&de5$pid==6]=6
de5$Pi[de5$Ey==1993&de5$pid==3]=1#1993:3,6,8,2,5,9
de5$Pi[de5$Ey==1993&de5$pid==6]=2
de5$Pi[de5$Ey==1993&de5$pid==8]=3
de5$Pi[de5$Ey==1993&de5$pid==2]=4
de5$Pi[de5$Ey==1993&de5$pid==5]=5
de5$Pi[de5$Ey==1993&de5$pid==9]=6
de5$Pi[de5$Ey==1997&de5$pid==3]=1#1997:3,9,5,8,6,2
de5$Pi[de5$Ey==1997&de5$pid==9]=2
de5$Pi[de5$Ey==1997&de5$pid==5]=3
de5$Pi[de5$Ey==1997&de5$pid==8]=4
de5$Pi[de5$Ey==1997&de5$pid==6]=5
de5$Pi[de5$Ey==1997&de5$pid==2]=6
#subset:
colnames(de5)
de5=de5[,c(1,2,3,132,128,131,133:144,151,47,145:150,152,57,153,154,161:174,7,175)]
colnames(de5)
de5[colnames(de5)]=sapply(de5[colnames(de5)],as.numeric)


#SWEDEN EV de6:----
de6=read_dta("ZA3911_se_1956-1998.dta")
colnames(de6)
colnames(de6)[colnames(de6)=="year"]="Ey"#Election Year
de6$Ey=as.numeric(de6$Ey)
de6=de6[de6$Ey>=1982,]#get rid of elections without likability
colnames(de6)[colnames(de6)=="country"]="Ec"#Election Country
de6$Ec=6
de6$Ec=as.numeric(de6$Ec)
colnames(de6)[colnames(de6)=="age"]="Rage"#Respondent Age
de6$Rage=as.numeric(de6$Rage)
de6$Rage[de6$Rage>100]=NA
#Vt#Vote Turnout (0 did not vote; 1 voted)
colnames(de6)[colnames(de6)=="vote"]="Vt"
de6$Vt[de6$Vt>2]=NA
table(de6$Vt)
#"Va"#Vote Alphabetical (here numbers), by vote share:
de6$Va=NA
de6$Va[de6$Ey==1982&de6$party==2]=1#1982:2,5,3,4,1
de6$Va[de6$Ey==1982&de6$party==5]=2
de6$Va[de6$Ey==1982&de6$party==3]=3
de6$Va[de6$Ey==1982&de6$party==4]=4
de6$Va[de6$Ey==1982&de6$party==1]=5
de6$Va[de6$Ey==1985&de6$party==2]=1#1985:2,5,4,3,1,10
de6$Va[de6$Ey==1985&de6$party==5]=2
de6$Va[de6$Ey==1985&de6$party==4]=3
de6$Va[de6$Ey==1985&de6$party==3]=4
de6$Va[de6$Ey==1985&de6$party==1]=5
de6$Va[de6$Ey==1985&de6$party==10]=6
de6$Va[de6$Ey==1988&de6$party==2]=1#1988:2,5,4,3,1
de6$Va[de6$Ey==1988&de6$party==5]=2
de6$Va[de6$Ey==1988&de6$party==4]=3
de6$Va[de6$Ey==1988&de6$party==3]=4
de6$Va[de6$Ey==1988&de6$party==1]=5
de6$Va[de6$Ey==1991&de6$party==2]=1#1991:2,5,4,3,10,12
de6$Va[de6$Ey==1991&de6$party==5]=2
de6$Va[de6$Ey==1991&de6$party==4]=3
de6$Va[de6$Ey==1991&de6$party==3]=4
de6$Va[de6$Ey==1991&de6$party==10]=5
de6$Va[de6$Ey==1991&de6$party==12]=6
de6$Va[de6$Ey==1994&de6$party==2]=1#1994:2,5,3,4,1,11
de6$Va[de6$Ey==1994&de6$party==5]=2
de6$Va[de6$Ey==1994&de6$party==3]=3
de6$Va[de6$Ey==1994&de6$party==4]=4
de6$Va[de6$Ey==1994&de6$party==1]=5
de6$Va[de6$Ey==1994&de6$party==11]=6
de6$Va[de6$Ey==1998&de6$party==2]=1#1998:2,5,1,10,3,4
de6$Va[de6$Ey==1998&de6$party==5]=2
de6$Va[de6$Ey==1998&de6$party==1]=3
de6$Va[de6$Ey==1998&de6$party==10]=4
de6$Va[de6$Ey==1998&de6$party==3]=5
de6$Va[de6$Ey==1998&de6$party==4]=6
de6$Rwd=1#Respondent Weight Demographic (there's no weight, all entries set to 1)
#Number of Parties:
de6$NP=NA
de6$NP[de6$Ey==1982]=5
de6$NP[de6$Ey==1985]=6
de6$NP[de6$Ey==1988]=5
de6$NP[de6$Ey==1991]=6
de6$NP[de6$Ey==1994]=6
de6$NP[de6$Ey==1998]=6
#party likability:
de6$Pl.1=NA
de6$Pl.2=NA
de6$Pl.3=NA
de6$Pl.4=NA
de6$Pl.5=NA
de6$Pl.6=NA
# de6$Pl.7=NA
table(de6$symp2[de6$Ey==1998])
de6$Pl.1[de6$Ey==1982]=de6$symp2[de6$Ey==1982]#1982:2,5,3,4,1
de6$Pl.2[de6$Ey==1982]=de6$symp5[de6$Ey==1982]
de6$Pl.3[de6$Ey==1982]=de6$symp3[de6$Ey==1982]
de6$Pl.4[de6$Ey==1982]=de6$symp4[de6$Ey==1982]
de6$Pl.5[de6$Ey==1982]=de6$symp1[de6$Ey==1982]
de6$Pl.1[de6$Ey==1985]=de6$symp2[de6$Ey==1985]#1985:2,5,4,3,1,10
de6$Pl.2[de6$Ey==1985]=de6$symp5[de6$Ey==1985]
de6$Pl.3[de6$Ey==1985]=de6$symp4[de6$Ey==1985]
de6$Pl.4[de6$Ey==1985]=de6$symp3[de6$Ey==1985]
de6$Pl.5[de6$Ey==1985]=de6$symp1[de6$Ey==1985]
de6$Pl.6[de6$Ey==1985]=de6$symp6[de6$Ey==1985]
de6$Pl.1[de6$Ey==1988]=de6$symp2[de6$Ey==1988]#1988:2,5,4,3,1
de6$Pl.2[de6$Ey==1988]=de6$symp5[de6$Ey==1988]
de6$Pl.3[de6$Ey==1988]=de6$symp4[de6$Ey==1988]
de6$Pl.4[de6$Ey==1988]=de6$symp3[de6$Ey==1988]
de6$Pl.5[de6$Ey==1988]=de6$symp1[de6$Ey==1988]
de6$Pl.1[de6$Ey==1991]=de6$symp2[de6$Ey==1991]#1991:2,5,4,3,10,12
de6$Pl.2[de6$Ey==1991]=de6$symp5[de6$Ey==1991]
de6$Pl.3[de6$Ey==1991]=de6$symp4[de6$Ey==1991]
de6$Pl.4[de6$Ey==1991]=de6$symp3[de6$Ey==1991]
de6$Pl.5[de6$Ey==1991]=de6$symp6[de6$Ey==1991]
de6$Pl.6[de6$Ey==1991]=de6$symp8[de6$Ey==1991]
de6$Pl.1[de6$Ey==1994]=de6$symp2[de6$Ey==1994]#1994:2,5,3,4,1,11
de6$Pl.2[de6$Ey==1994]=de6$symp5[de6$Ey==1994]
de6$Pl.3[de6$Ey==1994]=de6$symp3[de6$Ey==1994]
de6$Pl.4[de6$Ey==1994]=de6$symp4[de6$Ey==1994]
de6$Pl.5[de6$Ey==1994]=de6$symp1[de6$Ey==1994]
de6$Pl.6[de6$Ey==1994]=de6$symp7[de6$Ey==1994]
de6$Pl.1[de6$Ey==1998]=de6$symp2[de6$Ey==1998]#1998:2,5,1,10,3,4
de6$Pl.2[de6$Ey==1998]=de6$symp5[de6$Ey==1998]
de6$Pl.3[de6$Ey==1998]=de6$symp1[de6$Ey==1998]
de6$Pl.4[de6$Ey==1998]=de6$symp6[de6$Ey==1998]
de6$Pl.5[de6$Ey==1998]=de6$symp3[de6$Ey==1998]
de6$Pl.6[de6$Ey==1998]=de6$symp4[de6$Ey==1998]
de6$Pl.1[de6$Pl.1>5]=NA#recoding to 0-10 scale
de6$Pl.2[de6$Pl.2>5]=NA
de6$Pl.3[de6$Pl.3>5]=NA
de6$Pl.4[de6$Pl.4>5]=NA
de6$Pl.5[de6$Pl.5>5]=NA
de6$Pl.6[de6$Pl.6>5]=NA
de6$Pl.1=de6$Pl.1+5
de6$Pl.2=de6$Pl.2+5
de6$Pl.3=de6$Pl.3+5
de6$Pl.4=de6$Pl.4+5
de6$Pl.5=de6$Pl.5+5
de6$Pl.6=de6$Pl.6+5
#leader likability:
de6$Ll.1=NA
de6$Ll.2=NA
de6$Ll.3=NA
de6$Ll.4=NA
de6$Ll.5=NA
de6$Ll.6=NA
# de6$Ll.7=NA
de6$Ll.1[de6$Ey==1982]=de6$sympl2[de6$Ey==1982]#1982:2,5,3,4,1
de6$Ll.2[de6$Ey==1982]=de6$sympl5[de6$Ey==1982]
de6$Ll.3[de6$Ey==1982]=de6$sympl3[de6$Ey==1982]
de6$Ll.4[de6$Ey==1982]=de6$sympl4[de6$Ey==1982]
de6$Ll.5[de6$Ey==1982]=de6$sympl1[de6$Ey==1982]
de6$Ll.1[de6$Ey==1985]=de6$sympl2[de6$Ey==1985]#1985:2,5,4,3,1,10
de6$Ll.2[de6$Ey==1985]=de6$sympl5[de6$Ey==1985]
de6$Ll.3[de6$Ey==1985]=de6$sympl4[de6$Ey==1985]
de6$Ll.4[de6$Ey==1985]=de6$sympl3[de6$Ey==1985]
de6$Ll.5[de6$Ey==1985]=de6$sympl1[de6$Ey==1985]
de6$Ll.6[de6$Ey==1985]=de6$sympl6[de6$Ey==1985]
de6$Ll.1[de6$Ey==1988]=de6$sympl2[de6$Ey==1988]#1988:2,5,4,3,1
de6$Ll.2[de6$Ey==1988]=de6$sympl5[de6$Ey==1988]
de6$Ll.3[de6$Ey==1988]=de6$sympl4[de6$Ey==1988]
de6$Ll.4[de6$Ey==1988]=de6$sympl3[de6$Ey==1988]
de6$Ll.5[de6$Ey==1988]=de6$sympl1[de6$Ey==1988]
de6$Ll.1[de6$Ey==1991]=de6$sympl2[de6$Ey==1991]#1991:2,5,4,3,10,12
de6$Ll.2[de6$Ey==1991]=de6$sympl5[de6$Ey==1991]
de6$Ll.3[de6$Ey==1991]=de6$sympl4[de6$Ey==1991]
de6$Ll.4[de6$Ey==1991]=de6$sympl3[de6$Ey==1991]
de6$Ll.5[de6$Ey==1991]=de6$sympl6[de6$Ey==1991]
de6$Ll.6[de6$Ey==1991]=de6$sympl8a[de6$Ey==1991]
de6$Ll.1[de6$Ey==1994]=de6$sympl2[de6$Ey==1994]#1994:2,5,3,4,1,11
de6$Ll.2[de6$Ey==1994]=de6$sympl5[de6$Ey==1994]
de6$Ll.3[de6$Ey==1994]=de6$sympl3[de6$Ey==1994]
de6$Ll.4[de6$Ey==1994]=de6$sympl4[de6$Ey==1994]
de6$Ll.5[de6$Ey==1994]=de6$sympl1[de6$Ey==1994]
de6$Ll.6[de6$Ey==1994]=de6$sympl7[de6$Ey==1994]
de6$Ll.1[de6$Ey==1998]=de6$sympl2[de6$Ey==1998]#1998:2,5,1,10,3,4
de6$Ll.2[de6$Ey==1998]=de6$sympl5[de6$Ey==1998]
de6$Ll.3[de6$Ey==1998]=de6$sympl1[de6$Ey==1998]
de6$Ll.4[de6$Ey==1998]=de6$sympl6[de6$Ey==1998]
de6$Ll.5[de6$Ey==1998]=de6$sympl3[de6$Ey==1998]
de6$Ll.6[de6$Ey==1998]=de6$sympl4[de6$Ey==1998]
de6$Ll.1[de6$Ll.1>5]=NA#recoding to 0-10 scale
de6$Ll.2[de6$Ll.2>5]=NA
de6$Ll.3[de6$Ll.3>5]=NA
de6$Ll.4[de6$Ll.4>5]=NA
de6$Ll.5[de6$Ll.5>5]=NA
de6$Ll.6[de6$Ll.6>5]=NA
de6$Ll.1=de6$Ll.1+5
de6$Ll.2=de6$Ll.2+5
de6$Ll.3=de6$Ll.3+5
de6$Ll.4=de6$Ll.4+5
de6$Ll.5=de6$Ll.5+5
de6$Ll.6=de6$Ll.6+5
#LRR=Left Right Respondent (voter LR self):
colnames(de6)[colnames(de6)=="lrpown"]="LRR"
de6$LRR[de6$LRR>10]=NA#recoding to 0-10 scale
de6$LRR=as.numeric(de6$LRR)
#LRP.i=Left Right Parties:
de6$LRP.1=NA
de6$LRP.2=NA
de6$LRP.3=NA
de6$LRP.4=NA
de6$LRP.5=NA
de6$LRP.6=NA
# de6$LRP.7=NA
de6$LRP.1[de6$Ey==1982]=de6$lrp2[de6$Ey==1982]#1982:2,5,3,4,1
de6$LRP.2[de6$Ey==1982]=de6$lrp5[de6$Ey==1982]
de6$LRP.3[de6$Ey==1982]=de6$lrp3[de6$Ey==1982]
de6$LRP.4[de6$Ey==1982]=de6$lrp4[de6$Ey==1982]
de6$LRP.5[de6$Ey==1982]=de6$lrp1[de6$Ey==1982]
de6$LRP.1[de6$Ey==1985]=de6$lrp2[de6$Ey==1985]#1985:2,5,4,3,1,10
de6$LRP.2[de6$Ey==1985]=de6$lrp5[de6$Ey==1985]
de6$LRP.3[de6$Ey==1985]=de6$lrp4[de6$Ey==1985]
de6$LRP.4[de6$Ey==1985]=de6$lrp3[de6$Ey==1985]
de6$LRP.5[de6$Ey==1985]=de6$lrp1[de6$Ey==1985]
de6$LRP.6[de6$Ey==1985]=de6$lrp6[de6$Ey==1985]
de6$LRP.1[de6$Ey==1988]=de6$lrp2[de6$Ey==1988]#1988:2,5,4,3,1
de6$LRP.2[de6$Ey==1988]=de6$lrp5[de6$Ey==1988]
de6$LRP.3[de6$Ey==1988]=de6$lrp4[de6$Ey==1988]
de6$LRP.4[de6$Ey==1988]=de6$lrp3[de6$Ey==1988]
de6$LRP.5[de6$Ey==1988]=de6$lrp1[de6$Ey==1988]
de6$LRP.1[de6$Ey==1991]=de6$lrp2[de6$Ey==1991]#1991:2,5,4,3,10,12
de6$LRP.2[de6$Ey==1991]=de6$lrp5[de6$Ey==1991]
de6$LRP.3[de6$Ey==1991]=de6$lrp4[de6$Ey==1991]
de6$LRP.4[de6$Ey==1991]=de6$lrp3[de6$Ey==1991]
de6$LRP.5[de6$Ey==1991]=de6$lrp6[de6$Ey==1991]
de6$LRP.6[de6$Ey==1991]=de6$lrp8[de6$Ey==1991]
de6$LRP.1[de6$Ey==1994]=de6$lrp2[de6$Ey==1994]#1994:2,5,3,4,1,11
de6$LRP.2[de6$Ey==1994]=de6$lrp5[de6$Ey==1994]
de6$LRP.3[de6$Ey==1994]=de6$lrp3[de6$Ey==1994]
de6$LRP.4[de6$Ey==1994]=de6$lrp4[de6$Ey==1994]
de6$LRP.5[de6$Ey==1994]=de6$lrp1[de6$Ey==1994]
de6$LRP.6[de6$Ey==1994]=de6$lrp7[de6$Ey==1994]
de6$LRP.1[de6$Ey==1998]=de6$lrp2[de6$Ey==1998]#1998:2,5,1,10,3,4
de6$LRP.2[de6$Ey==1998]=de6$lrp5[de6$Ey==1998]
de6$LRP.3[de6$Ey==1998]=de6$lrp1[de6$Ey==1998]
de6$LRP.4[de6$Ey==1998]=de6$lrp6[de6$Ey==1998]
de6$LRP.5[de6$Ey==1998]=de6$lrp3[de6$Ey==1998]
de6$LRP.6[de6$Ey==1998]=de6$lrp4[de6$Ey==1998]
de6$LRP.1[de6$LRP.1>10]=NA#recoding to 0-10 scale
de6$LRP.2[de6$LRP.2>10]=NA
de6$LRP.3[de6$LRP.3>10]=NA
de6$LRP.4[de6$LRP.4>10]=NA
de6$LRP.5[de6$LRP.5>10]=NA
de6$LRP.6[de6$LRP.6>10]=NA
#Elections with LRR and LRP:
de6$LR=NA
de6$LR[de6$Ey==1982]=1
de6$LR[de6$Ey==1985]=1
de6$LR[de6$Ey==1988]=1
de6$LR[de6$Ey==1991]=1
de6$LR[de6$Ey==1994]=1
de6$LR[de6$Ey==1998]=1
#EeconS=Election economy state Subjective:(Retrospective socio-tropic economic evaluations)
# 1. MUCH BETTER                 
# 3. STAYED THE SAME              
# 5. MUCH WORSE
colnames(de6)[colnames(de6)=="econ12"]="EeconS"
de6$EeconS[de6$EeconS>5]=NA
de6$EeconS[de6$Ey==1982]=NA#scale in the data different than scale in the codebook
#recode EeconS from EV scale
# 1. GOTTEN BETTER c(1,2,3,4,5)
# 5. GOTTEN WORSE 
# to TEV scale, noting that the TEV used the conversion scale below for same elections:
# 0. Negative evaluation c(0, 0.25, 0.5, 0.75, 1)
# 1. Positive evaluation
de6$EeconS[de6$EeconS==1]=1
de6$EeconS[de6$EeconS==2]=0.75
de6$EeconS[de6$EeconS==3]=0.5
de6$EeconS[de6$EeconS==4]=0.25
de6$EeconS[de6$EeconS==5]=0
#Elections with EeconS:
de6$Eecon=NA
de6$Eecon[de6$Ey==1982]=0
de6$Eecon[de6$Ey==1985]=1
de6$Eecon[de6$Ey==1988]=1
de6$Eecon[de6$Ey==1991]=1
de6$Eecon[de6$Ey==1994]=1
de6$Eecon[de6$Ey==1998]=1
#Seats to vote ratios:
de6$P1P2rat=NA
de6$P1P2rat[de6$Ey==1982]=1.00
de6$P1P2rat[de6$Ey==1985]=1.00
de6$P1P2rat[de6$Ey==1988]=1.00
de6$P1P2rat[de6$Ey==1991]=1.00
de6$P1P2rat[de6$Ey==1994]=1.00
de6$P1P2rat[de6$Ey==1998]=1.01
#Gallagher Disproportionality measure:
de6$GDM=NA
de6$GDM[de6$Ey==1982]=2.40
de6$GDM[de6$Ey==1985]=1.35
de6$GDM[de6$Ey==1988]=2.45
de6$GDM[de6$Ey==1991]=2.86
de6$GDM[de6$Ey==1994]=1.18
de6$GDM[de6$Ey==1998]=0.97
#Party family:
de6$PartyFamilyCode1=20
de6$PartyFamilyCode2=30
de6$PartyFamilyCode3=80
de6$PartyFamilyCode4=40
de6$PartyFamilyCode5=60
de6$PartyFamilyCode6=50
de6$PartyFamilyCode7=10
de6$PartyFamilyCode8=95
# 1 Left Party (Vänsterpartiet)
# 2 Social Democrats (Socialdemokraterna)
# 3 Center Party (Centerpartiet)
# 4 People’s Party Liberals (Folkpartiet Liberalerna)
# 5 Moderate Party (Moderata Samlingspartiet)
# 10 (6) Christian Democrats (Kristdemokraterna)
# 11 (7) Green Party (Miljöpartiet de Gröna)
# 12 (8) New Democracy (Ny Demokrati)
de6$Pf.1=NA
de6$Pf.2=NA
de6$Pf.3=NA
de6$Pf.4=NA
de6$Pf.5=NA
de6$Pf.6=NA
# de6$Pf.7=NA
de6$Pf.1[de6$Ey==1982]=de6$PartyFamilyCode2[de6$Ey==1982]#1982:2,5,3,4,1
de6$Pf.2[de6$Ey==1982]=de6$PartyFamilyCode5[de6$Ey==1982]
de6$Pf.3[de6$Ey==1982]=de6$PartyFamilyCode3[de6$Ey==1982]
de6$Pf.4[de6$Ey==1982]=de6$PartyFamilyCode4[de6$Ey==1982]
de6$Pf.5[de6$Ey==1982]=de6$PartyFamilyCode1[de6$Ey==1982]
de6$Pf.1[de6$Ey==1985]=de6$PartyFamilyCode2[de6$Ey==1985]#1985:2,5,4,3,1,10
de6$Pf.2[de6$Ey==1985]=de6$PartyFamilyCode5[de6$Ey==1985]
de6$Pf.3[de6$Ey==1985]=de6$PartyFamilyCode4[de6$Ey==1985]
de6$Pf.4[de6$Ey==1985]=de6$PartyFamilyCode3[de6$Ey==1985]
de6$Pf.5[de6$Ey==1985]=de6$PartyFamilyCode1[de6$Ey==1985]
de6$Pf.6[de6$Ey==1985]=de6$PartyFamilyCode6[de6$Ey==1985]
de6$Pf.1[de6$Ey==1988]=de6$PartyFamilyCode2[de6$Ey==1988]#1988:2,5,4,3,1
de6$Pf.2[de6$Ey==1988]=de6$PartyFamilyCode5[de6$Ey==1988]
de6$Pf.3[de6$Ey==1988]=de6$PartyFamilyCode4[de6$Ey==1988]
de6$Pf.4[de6$Ey==1988]=de6$PartyFamilyCode3[de6$Ey==1988]
de6$Pf.5[de6$Ey==1988]=de6$PartyFamilyCode1[de6$Ey==1988]
de6$Pf.1[de6$Ey==1991]=de6$PartyFamilyCode2[de6$Ey==1991]#1991:2,5,4,3,10,12
de6$Pf.2[de6$Ey==1991]=de6$PartyFamilyCode5[de6$Ey==1991]
de6$Pf.3[de6$Ey==1991]=de6$PartyFamilyCode4[de6$Ey==1991]
de6$Pf.4[de6$Ey==1991]=de6$PartyFamilyCode3[de6$Ey==1991]
de6$Pf.5[de6$Ey==1991]=de6$PartyFamilyCode6[de6$Ey==1991]
de6$Pf.6[de6$Ey==1991]=de6$PartyFamilyCode8[de6$Ey==1991]
de6$Pf.1[de6$Ey==1994]=de6$PartyFamilyCode2[de6$Ey==1994]#1994:2,5,3,4,1,11
de6$Pf.2[de6$Ey==1994]=de6$PartyFamilyCode5[de6$Ey==1994]
de6$Pf.3[de6$Ey==1994]=de6$PartyFamilyCode3[de6$Ey==1994]
de6$Pf.4[de6$Ey==1994]=de6$PartyFamilyCode4[de6$Ey==1994]
de6$Pf.5[de6$Ey==1994]=de6$PartyFamilyCode1[de6$Ey==1994]
de6$Pf.6[de6$Ey==1994]=de6$PartyFamilyCode7[de6$Ey==1994]
de6$Pf.1[de6$Ey==1998]=de6$PartyFamilyCode2[de6$Ey==1998]#1998:2,5,1,10,3,4
de6$Pf.2[de6$Ey==1998]=de6$PartyFamilyCode5[de6$Ey==1998]
de6$Pf.3[de6$Ey==1998]=de6$PartyFamilyCode1[de6$Ey==1998]
de6$Pf.4[de6$Ey==1998]=de6$PartyFamilyCode6[de6$Ey==1998]
de6$Pf.5[de6$Ey==1998]=de6$PartyFamilyCode3[de6$Ey==1998]
de6$Pf.6[de6$Ey==1998]=de6$PartyFamilyCode4[de6$Ey==1998]
#coding coalition status:
de6$CoalCode=0
#CMPid.i:
de6$CMPid.1=NA#create CMPid:
de6$CMPid.2=NA
de6$CMPid.3=NA
de6$CMPid.4=NA
de6$CMPid.5=NA
de6$CMPid.6=NA
de6$CMPid.1=as.numeric(de6$CMPid.1)#recode as numeric:
de6$CMPid.2=as.numeric(de6$CMPid.2)
de6$CMPid.3=as.numeric(de6$CMPid.3)
de6$CMPid.4=as.numeric(de6$CMPid.4)
de6$CMPid.5=as.numeric(de6$CMPid.5)
de6$CMPid.6=as.numeric(de6$CMPid.6)
#PrePost:
de6$PrePost=as.numeric(NA)
#"Pi"#Party identification:
de6$Pi=NA
de6$Pi[de6$Ey==1982&de6$pid==2]=1#1982:2,5,3,4,1
de6$Pi[de6$Ey==1982&de6$pid==5]=2
de6$Pi[de6$Ey==1982&de6$pid==3]=3
de6$Pi[de6$Ey==1982&de6$pid==4]=4
de6$Pi[de6$Ey==1982&de6$pid==1]=5
de6$Pi[de6$Ey==1985&de6$pid==2]=1#1985:2,5,4,3,1,10
de6$Pi[de6$Ey==1985&de6$pid==5]=2
de6$Pi[de6$Ey==1985&de6$pid==4]=3
de6$Pi[de6$Ey==1985&de6$pid==3]=4
de6$Pi[de6$Ey==1985&de6$pid==1]=5
de6$Pi[de6$Ey==1985&de6$pid==10]=6
de6$Pi[de6$Ey==1988&de6$pid==2]=1#1988:2,5,4,3,1
de6$Pi[de6$Ey==1988&de6$pid==5]=2
de6$Pi[de6$Ey==1988&de6$pid==4]=3
de6$Pi[de6$Ey==1988&de6$pid==3]=4
de6$Pi[de6$Ey==1988&de6$pid==1]=5
de6$Pi[de6$Ey==1991&de6$pid==2]=1#1991:2,5,4,3,10,12
de6$Pi[de6$Ey==1991&de6$pid==5]=2
de6$Pi[de6$Ey==1991&de6$pid==4]=3
de6$Pi[de6$Ey==1991&de6$pid==3]=4
de6$Pi[de6$Ey==1991&de6$pid==10]=5
de6$Pi[de6$Ey==1991&de6$pid==12]=6
de6$Pi[de6$Ey==1994&de6$pid==2]=1#1994:2,5,3,4,1,11
de6$Pi[de6$Ey==1994&de6$pid==5]=2
de6$Pi[de6$Ey==1994&de6$pid==3]=3
de6$Pi[de6$Ey==1994&de6$pid==4]=4
de6$Pi[de6$Ey==1994&de6$pid==1]=5
de6$Pi[de6$Ey==1994&de6$pid==11]=6
de6$Pi[de6$Ey==1998&de6$pid==2]=1#1998:2,5,1,10,3,4
de6$Pi[de6$Ey==1998&de6$pid==5]=2
de6$Pi[de6$Ey==1998&de6$pid==1]=3
de6$Pi[de6$Ey==1998&de6$pid==10]=4
de6$Pi[de6$Ey==1998&de6$pid==3]=5
de6$Pi[de6$Ey==1998&de6$pid==4]=6
#subset:
colnames(de6)
de6=de6[,c(3,2,308,309,18,307,310:321,328,48,322:327,329,123,330,331,340:353,22,354)]
colnames(de6)
de6[colnames(de6)]=sapply(de6[colnames(de6)],as.numeric)


#AUSTRALIA LP dl7:----
dl7=dl[dl$Ec==1,]#create country dataset
#first reshape to wide dataset:
dl7$Vn[dl7$Vn==10]=NA#set Vn==NA for NAs
dl7$Rid=paste0(dl7$Ey,dl7$Rid)#create unique Rid
colnames(dl7)
dl7[c(which(duplicated(dl7[,c(10,12)]))),]#rows with duplicated Rid/PartyID observations
unique(dl7$Rid[duplicated(dl7[,c(10,12)])])#Rid for rows with duplicated Rid/PartyID observations
dl7[dl7$Rid==1987805,]
dl7[dl7$Rid==19871442,]
dl7[dl7$Rid==19871815,]
dl7[dl7$Rid==19872176,]
dl7[dl7$Rid==19872263,]
#all the duplicate Rid/PartyID in dl7 have Pl==NA therefore will be excluded later on
dl7=dl7 %>% distinct(Rid,PartyID,.keep_all = TRUE)#get rid of duplicates
dl7=dcast(setDT(dl7), Ec+Ey+Rwd+Vt+Vn+LRR+Rid+Pi ~ PartyID, value.var = c("Pl","Ll","LRP","Ps"))#create wide dataset
#then code country:
dl7$Ey=as.numeric(dl7$Ey)
dl7=dl7[!(dl7$Ey %in% c(1987,1990)),]#get rid of elections without likability
dl7$Ec=as.numeric(dl7$Ec)
dl7$Ec=7
dl7$Rage=as.numeric(NA)#Respondent Age is not coded in LP dataset
#Vt#Vote Turnout (0 did not vote; 1 voted)
dl7$Vt=as.numeric(dl7$Vt)
#"Va"#Vote Alphabetical (here numbers), by vote share:
#(single parties for vote alphabetical)
# # 11         LP (Aus) Liberal - Vn==1
# # 12        ALP (Aus) Labor - Vn==2
# # 13         NP (Aus) Nationals - Vn==3
# # 14         AD (Aus) Democrats - Vn==4
dl7$Va=NA
dl7$Va[dl7$Ey==1993&dl7$Vn==2]=1#1993:12,11,13,14
dl7$Va[dl7$Ey==1993&dl7$Vn==1]=2
dl7$Va[dl7$Ey==1993&dl7$Vn==3]=3
dl7$Va[dl7$Ey==1993&dl7$Vn==4]=4
dl7$Va[dl7$Ey==1996&dl7$Vn==2]=1#1996:12,11,13,14
dl7$Va[dl7$Ey==1996&dl7$Vn==1]=2
dl7$Va[dl7$Ey==1996&dl7$Vn==3]=3
dl7$Va[dl7$Ey==1996&dl7$Vn==4]=4
dl7$Va[dl7$Ey==1998&dl7$Vn==2]=1#1998:12,11,13,14
dl7$Va[dl7$Ey==1998&dl7$Vn==1]=2
dl7$Va[dl7$Ey==1998&dl7$Vn==3]=3
dl7$Va[dl7$Ey==1998&dl7$Vn==4]=4
dl7$Va[dl7$Ey==2001&dl7$Vn==2]=1#2001:12,11,13,14
dl7$Va[dl7$Ey==2001&dl7$Vn==1]=2
dl7$Va[dl7$Ey==2001&dl7$Vn==3]=3
dl7$Va[dl7$Ey==2001&dl7$Vn==4]=4
dl7$Rwd=as.numeric(dl7$Rwd)#there is no information on weights, all are 1
#Number of Parties:
dl7$NP=NA
dl7$NP[dl7$Ey==1993]=4
dl7$NP[dl7$Ey==1996]=4
dl7$NP[dl7$Ey==1998]=4
dl7$NP[dl7$Ey==2001]=4
#party likability:
dl7$Pl.1=NA
dl7$Pl.2=NA
dl7$Pl.3=NA
dl7$Pl.4=NA
dl7$Pl.5=NA
dl7$Pl.6=NA
# dl7$Pl.7=NA
dl7$Pl.1[dl7$Ey==1993]=dl7$Pl_12[dl7$Ey==1993]#1993:12,11,13,14
dl7$Pl.2[dl7$Ey==1993]=dl7$Pl_11[dl7$Ey==1993]
dl7$Pl.3[dl7$Ey==1993]=dl7$Pl_13[dl7$Ey==1993]
dl7$Pl.4[dl7$Ey==1993]=dl7$Pl_14[dl7$Ey==1993]
dl7$Pl.1[dl7$Ey==1996]=dl7$Pl_12[dl7$Ey==1996]#1996:12,11,13,14
dl7$Pl.2[dl7$Ey==1996]=dl7$Pl_11[dl7$Ey==1996]
dl7$Pl.3[dl7$Ey==1996]=dl7$Pl_13[dl7$Ey==1996]
dl7$Pl.4[dl7$Ey==1996]=dl7$Pl_14[dl7$Ey==1996]
dl7$Pl.1[dl7$Ey==1998]=dl7$Pl_12[dl7$Ey==1998]#1998:12,11,13,14
dl7$Pl.2[dl7$Ey==1998]=dl7$Pl_11[dl7$Ey==1998]
dl7$Pl.3[dl7$Ey==1998]=dl7$Pl_13[dl7$Ey==1998]
dl7$Pl.4[dl7$Ey==1998]=dl7$Pl_14[dl7$Ey==1998]
dl7$Pl.1[dl7$Ey==2001]=dl7$Pl_12[dl7$Ey==2001]#2001:12,11,13,14
dl7$Pl.2[dl7$Ey==2001]=dl7$Pl_11[dl7$Ey==2001]
dl7$Pl.3[dl7$Ey==2001]=dl7$Pl_13[dl7$Ey==2001]
dl7$Pl.4[dl7$Ey==2001]=dl7$Pl_14[dl7$Ey==2001]
#already 0-10 scale
#leader likability:
dl7$Ll.1=NA
dl7$Ll.2=NA
dl7$Ll.3=NA
dl7$Ll.4=NA
dl7$Ll.5=NA
dl7$Ll.6=NA
# dl7$Ll.7=NA
dl7$Ll.1[dl7$Ey==1993]=dl7$Ll_12[dl7$Ey==1993]#1993:12,11,13,14
dl7$Ll.2[dl7$Ey==1993]=dl7$Ll_11[dl7$Ey==1993]
dl7$Ll.3[dl7$Ey==1993]=dl7$Ll_13[dl7$Ey==1993]
dl7$Ll.4[dl7$Ey==1993]=dl7$Ll_14[dl7$Ey==1993]
dl7$Ll.1[dl7$Ey==1996]=dl7$Ll_12[dl7$Ey==1996]#1996:12,11,13,14
dl7$Ll.2[dl7$Ey==1996]=dl7$Ll_11[dl7$Ey==1996]
dl7$Ll.3[dl7$Ey==1996]=dl7$Ll_13[dl7$Ey==1996]
dl7$Ll.4[dl7$Ey==1996]=dl7$Ll_14[dl7$Ey==1996]
dl7$Ll.1[dl7$Ey==1998]=dl7$Ll_12[dl7$Ey==1998]#1998:12,11,13,14
dl7$Ll.2[dl7$Ey==1998]=dl7$Ll_11[dl7$Ey==1998]
dl7$Ll.3[dl7$Ey==1998]=dl7$Ll_13[dl7$Ey==1998]
dl7$Ll.4[dl7$Ey==1998]=dl7$Ll_14[dl7$Ey==1998]
dl7$Ll.1[dl7$Ey==2001]=dl7$Ll_12[dl7$Ey==2001]#2001:12,11,13,14
dl7$Ll.2[dl7$Ey==2001]=dl7$Ll_11[dl7$Ey==2001]
dl7$Ll.3[dl7$Ey==2001]=dl7$Ll_13[dl7$Ey==2001]
dl7$Ll.4[dl7$Ey==2001]=dl7$Ll_14[dl7$Ey==2001]
#already 0-10 scale
#LRR=Left Right Respondent (voter LR self):
dl7$LRR=as.numeric(dl7$LRR)
#already 0-10 scale
#LRP.i=Left Right Parties:
dl7$LRP.1=NA
dl7$LRP.2=NA
dl7$LRP.3=NA
dl7$LRP.4=NA
dl7$LRP.5=NA
dl7$LRP.6=NA
# dl7$LRP.7=NA
dl7$LRP.1[dl7$Ey==1993]=dl7$LRP_12[dl7$Ey==1993]#1993:12,11,13,14
dl7$LRP.2[dl7$Ey==1993]=dl7$LRP_11[dl7$Ey==1993]
dl7$LRP.3[dl7$Ey==1993]=dl7$LRP_13[dl7$Ey==1993]
dl7$LRP.4[dl7$Ey==1993]=dl7$LRP_14[dl7$Ey==1993]
dl7$LRP.1[dl7$Ey==1996]=dl7$LRP_12[dl7$Ey==1996]#1996:12,11,13,14
dl7$LRP.2[dl7$Ey==1996]=dl7$LRP_11[dl7$Ey==1996]
dl7$LRP.3[dl7$Ey==1996]=dl7$LRP_13[dl7$Ey==1996]
dl7$LRP.4[dl7$Ey==1996]=dl7$LRP_14[dl7$Ey==1996]
dl7$LRP.1[dl7$Ey==1998]=dl7$LRP_12[dl7$Ey==1998]#1998:12,11,13,14
dl7$LRP.2[dl7$Ey==1998]=dl7$LRP_11[dl7$Ey==1998]
dl7$LRP.3[dl7$Ey==1998]=dl7$LRP_13[dl7$Ey==1998]
dl7$LRP.4[dl7$Ey==1998]=dl7$LRP_14[dl7$Ey==1998]
dl7$LRP.1[dl7$Ey==2001]=dl7$LRP_12[dl7$Ey==2001]#2001:12,11,13,14
dl7$LRP.2[dl7$Ey==2001]=dl7$LRP_11[dl7$Ey==2001]
dl7$LRP.3[dl7$Ey==2001]=dl7$LRP_13[dl7$Ey==2001]
dl7$LRP.4[dl7$Ey==2001]=dl7$LRP_14[dl7$Ey==2001]
#already 0-10 scale
#Elections with LRR and LRP:
dl7$LR=NA
dl7$LR[dl7$Ey==1993]=0
dl7$LR[dl7$Ey==1996]=1
dl7$LR[dl7$Ey==1998]=1
dl7$LR[dl7$Ey==2001]=1
#EeconS=Election economy state Subjective:(Retrospective socio-tropic economic evaluations)
dl7$EeconS=NA#(no data)
#Elections with EeconS:
dl7$Eecon=0#(no data)
#Seats to vote ratios:
dl7$P1P2rat=NA
dl7$P1P2rat[dl7$Ey==1993]=1.21#recoding AUS_1996 percent vote: Liberal and National in coalition becoming
dl7$P1P2rat[dl7$Ey==1996]=1.57#the biggest (party A); and the Labor Party the second (party B); but ONLY for the shares
dl7$P1P2rat[dl7$Ey==1998]=1.24#(by seats)
dl7$P1P2rat[dl7$Ey==2001]=1.11
#Gallagher Disproportionality measure:
dl7$GDM=NA
dl7$GDM[dl7$Ey==1993]=8.12
dl7$GDM[dl7$Ey==1996]=8.12
dl7$GDM[dl7$Ey==1998]=10.87
dl7$GDM[dl7$Ey==2001]=9.37
#Party family:
dl7$PartyFamilyCode11=60
dl7$PartyFamilyCode12=30
dl7$PartyFamilyCode13=80
dl7$PartyFamilyCode14=30
#"Va"#Vote Alphabetical (here numbers), by vote share:
#(single parties for vote alphabetical)
# # 11         LP (Aus) Liberal - Vn==1
# # 12        ALP (Aus) Labor - Vn==2
# # 13         NP (Aus) Nationals - Vn==3
# # 14         AD (Aus) Democrats - Vn==4
dl7$Pf.1=NA
dl7$Pf.2=NA
dl7$Pf.3=NA
dl7$Pf.4=NA
dl7$Pf.5=NA
dl7$Pf.6=NA
# dl7$Pf.7=NA
dl7$Pf.1[dl7$Ey==1993]=dl7$PartyFamilyCode12[dl7$Ey==1993]#1993:12,11,13,14
dl7$Pf.2[dl7$Ey==1993]=dl7$PartyFamilyCode11[dl7$Ey==1993]
dl7$Pf.3[dl7$Ey==1993]=dl7$PartyFamilyCode13[dl7$Ey==1993]
dl7$Pf.4[dl7$Ey==1993]=dl7$PartyFamilyCode14[dl7$Ey==1993]
dl7$Pf.1[dl7$Ey==1996]=dl7$PartyFamilyCode12[dl7$Ey==1996]#1996:12,11,13,14
dl7$Pf.2[dl7$Ey==1996]=dl7$PartyFamilyCode11[dl7$Ey==1996]
dl7$Pf.3[dl7$Ey==1996]=dl7$PartyFamilyCode13[dl7$Ey==1996]
dl7$Pf.4[dl7$Ey==1996]=dl7$PartyFamilyCode14[dl7$Ey==1996]
dl7$Pf.1[dl7$Ey==1998]=dl7$PartyFamilyCode12[dl7$Ey==1998]#1998:12,11,13,14
dl7$Pf.2[dl7$Ey==1998]=dl7$PartyFamilyCode11[dl7$Ey==1998]
dl7$Pf.3[dl7$Ey==1998]=dl7$PartyFamilyCode13[dl7$Ey==1998]
dl7$Pf.4[dl7$Ey==1998]=dl7$PartyFamilyCode14[dl7$Ey==1998]
dl7$Pf.1[dl7$Ey==2001]=dl7$PartyFamilyCode12[dl7$Ey==2001]#2001:12,11,13,14
dl7$Pf.2[dl7$Ey==2001]=dl7$PartyFamilyCode11[dl7$Ey==2001]
dl7$Pf.3[dl7$Ey==2001]=dl7$PartyFamilyCode13[dl7$Ey==2001]
dl7$Pf.4[dl7$Ey==2001]=dl7$PartyFamilyCode14[dl7$Ey==2001]
#coding coalition status:
dl7$CoalCode=1
#CMPid.i:
dl7$CMPid.1=NA#create CMPid:
dl7$CMPid.2=NA
dl7$CMPid.3=NA
dl7$CMPid.4=NA
dl7$CMPid.5=NA
dl7$CMPid.6=NA
dl7$CMPid.1[dl7$Ey==1993]=63320#assign CMPid by party size order for parties with likability:
dl7$CMPid.2[dl7$Ey==1993]=63620
dl7$CMPid.3[dl7$Ey==1993]=63810
dl7$CMPid.4[dl7$Ey==1993]=NA
dl7$CMPid.1[dl7$Ey==1998]=63320
dl7$CMPid.2[dl7$Ey==1998]=63620
dl7$CMPid.3[dl7$Ey==1998]=63810
dl7$CMPid.4[dl7$Ey==1998]=63321
dl7$CMPid.1[dl7$Ey==2001]=63320
dl7$CMPid.2[dl7$Ey==2001]=63620
dl7$CMPid.3[dl7$Ey==2001]=63810
dl7$CMPid.4[dl7$Ey==2001]=NA
dl7$CMPid.1=as.numeric(dl7$CMPid.1)#recode as numeric:
dl7$CMPid.2=as.numeric(dl7$CMPid.2)
dl7$CMPid.3=as.numeric(dl7$CMPid.3)
dl7$CMPid.4=as.numeric(dl7$CMPid.4)
dl7$CMPid.5=as.numeric(dl7$CMPid.5)
dl7$CMPid.6=as.numeric(dl7$CMPid.6)
#PrePost:
dl7$PrePost=as.numeric(NA)
dl7$PrePost[dl7$Ey==1993]=1
#"Pi"#Party identification:
dl7$Pitemp=dl7$Pi#set Pi to Pitemp for ordering Pi by seat share
dl7$Pi=NA
dl7$Pi[dl7$Ey==1993&dl7$Pitemp==2]=1#1993:12,11,13,14
dl7$Pi[dl7$Ey==1993&dl7$Pitemp==1]=2
dl7$Pi[dl7$Ey==1993&dl7$Pitemp==3]=3
dl7$Pi[dl7$Ey==1993&dl7$Pitemp==4]=4
# dl7$Pi[dl7$Ey==1961&dl7$Pitemp<97&!(dl7$Pitemp %in% c(2,1,3))]=7#INCLUDING PARTY #7: NOT DONE FROM NOW ON!!!
dl7$Pi[dl7$Ey==1996&dl7$Pitemp==2]=1#1996:12,11,13,14
dl7$Pi[dl7$Ey==1996&dl7$Pitemp==1]=2
dl7$Pi[dl7$Ey==1996&dl7$Pitemp==3]=3
dl7$Pi[dl7$Ey==1996&dl7$Pitemp==4]=4
dl7$Pi[dl7$Ey==1998&dl7$Pitemp==2]=1#1998:12,11,13,14
dl7$Pi[dl7$Ey==1998&dl7$Pitemp==1]=2
dl7$Pi[dl7$Ey==1998&dl7$Pitemp==3]=3
dl7$Pi[dl7$Ey==1998&dl7$Pitemp==4]=4
dl7$Pi[dl7$Ey==2001&dl7$Pitemp==2]=1#2001:12,11,13,14
dl7$Pi[dl7$Ey==2001&dl7$Pitemp==1]=2
dl7$Pi[dl7$Ey==2001&dl7$Pitemp==3]=3
dl7$Pi[dl7$Ey==2001&dl7$Pitemp==4]=4
dl7$Pi=as.numeric(dl7$Pi)
#subset:
colnames(dl7)
dl7=dl7[,c(1,2,3,27,4,26,28:39,46,6,40:45,48,47,49,50,55:68,25,8)]
dl7=as.data.frame(dl7)
colnames(dl7)
# dl7[colnames(dl7)]=sapply(dl7[colnames(dl7)],as.numeric)


#CANADA LP dl8:----
dl8=dl[dl$Ec==2,]#create country dataset
#first reshape to wide dataset:
dl8$Vn[dl8$Vn==10]=NA#set Vn==NA for NAs
dl8$Rid=paste0(dl8$Ey,dl8$Rid)#create unique Rid
dl8=dl8[dl8$Ey!=1980,]#there is not Rid in dl8 1980
colnames(dl8)
dl8[c(which(duplicated(dl8[,c(10,12)]))),]#rows with duplicated Rid/PartyID observations
unique(dl8$Rid[duplicated(dl8[,c(10,12)])])#Rid for rows with duplicated Rid/PartyID observations
#there is no duplicate Rid/PartyID in dl8
dl8=dl8 %>% distinct(Rid,PartyID,.keep_all = TRUE)#get rid of duplicates (redundant)
dl8=dcast(setDT(dl8), Ec+Ey+Rwd+Vt+Vn+LRR+Rid+Pi ~ PartyID, value.var = c("Pl","Ll","LRP","Ps"))#create wide dataset
#then code country:
dl8$Ey=as.numeric(dl8$Ey)
dl8$Ec=as.numeric(dl8$Ec)
dl8$Ec=8
dl8$Rage=as.numeric(NA)#Respondent Age is not coded in LP dataset
#Vt#Vote Turnout (0 did not vote; 1 voted)
dl8$Vt=as.numeric(dl8$Vt)
#"Va"#Vote Alphabetical (here numbers), by vote share:
#(single parties for vote alphabetical)
# # 21        LPC (Can) Liberal - Vn==1
# # 22         PC (Can) Progressive Conservative - Vn==2
# # 23        NDP (Can) New Democratic - Vn==3
# # 24         CA (Can) Reform - Vn==4
# # 25         BQ (Can) Bloc Québécois - Vn==5
dl8$Va=NA
dl8$Va[dl8$Ey==1968&dl8$Vn==1]=1#1968:21,22,23
dl8$Va[dl8$Ey==1968&dl8$Vn==2]=2
dl8$Va[dl8$Ey==1968&dl8$Vn==3]=3
dl8$Va[dl8$Ey==1974&dl8$Vn==1]=1#1974:21,22,23
dl8$Va[dl8$Ey==1974&dl8$Vn==2]=2
dl8$Va[dl8$Ey==1974&dl8$Vn==3]=3
dl8$Va[dl8$Ey==1979&dl8$Vn==1]=1#1979:21,22,23(by vote, not seats, as CSES)
dl8$Va[dl8$Ey==1979&dl8$Vn==2]=2
dl8$Va[dl8$Ey==1979&dl8$Vn==3]=3
dl8$Va[dl8$Ey==1988&dl8$Vn==2]=1#1988:22,21,23
dl8$Va[dl8$Ey==1988&dl8$Vn==1]=2
dl8$Va[dl8$Ey==1988&dl8$Vn==3]=3
dl8$Va[dl8$Ey==1993&dl8$Vn==1]=1#1993:21,24,22,25,23(by vote, not seats, as CSES)
dl8$Va[dl8$Ey==1993&dl8$Vn==4]=2
dl8$Va[dl8$Ey==1993&dl8$Vn==2]=3
dl8$Va[dl8$Ey==1993&dl8$Vn==5]=4
dl8$Va[dl8$Ey==1993&dl8$Vn==3]=5
dl8$Va[dl8$Ey==1997&dl8$Vn==1]=1#1997:21,24,22,23,25(by vote, not seats, as CSES)
dl8$Va[dl8$Ey==1997&dl8$Vn==4]=2
dl8$Va[dl8$Ey==1997&dl8$Vn==2]=3
dl8$Va[dl8$Ey==1997&dl8$Vn==3]=4
dl8$Va[dl8$Ey==1997&dl8$Vn==5]=5
dl8$Rwd=as.numeric(dl8$Rwd)
#Number of Parties:
dl8$NP=NA
dl8$NP[dl8$Ey==1968]=3
dl8$NP[dl8$Ey==1974]=3
dl8$NP[dl8$Ey==1979]=3
dl8$NP[dl8$Ey==1988]=3
dl8$NP[dl8$Ey==1993]=5
dl8$NP[dl8$Ey==1997]=5
#party likability:
dl8$Pl.1=NA
dl8$Pl.2=NA
dl8$Pl.3=NA
dl8$Pl.4=NA
dl8$Pl.5=NA
dl8$Pl.6=NA
# dl8$Pl.7=NA
dl8$Pl.1[dl8$Ey==1968]=dl8$Pl_21[dl8$Ey==1968]#1968:21,22,23
dl8$Pl.2[dl8$Ey==1968]=dl8$Pl_22[dl8$Ey==1968]
dl8$Pl.3[dl8$Ey==1968]=dl8$Pl_23[dl8$Ey==1968]
dl8$Pl.1[dl8$Ey==1974]=dl8$Pl_21[dl8$Ey==1974]#1974:21,22,23
dl8$Pl.2[dl8$Ey==1974]=dl8$Pl_22[dl8$Ey==1974]
dl8$Pl.3[dl8$Ey==1974]=dl8$Pl_23[dl8$Ey==1974]
dl8$Pl.1[dl8$Ey==1979]=dl8$Pl_21[dl8$Ey==1979]#1979:21,22,23(by vote, not seats, as CSES)
dl8$Pl.2[dl8$Ey==1979]=dl8$Pl_22[dl8$Ey==1979]
dl8$Pl.3[dl8$Ey==1979]=dl8$Pl_23[dl8$Ey==1979]
dl8$Pl.1[dl8$Ey==1988]=dl8$Pl_22[dl8$Ey==1988]#1988:22,21,23
dl8$Pl.2[dl8$Ey==1988]=dl8$Pl_21[dl8$Ey==1988]
dl8$Pl.3[dl8$Ey==1988]=dl8$Pl_23[dl8$Ey==1988]
dl8$Pl.1[dl8$Ey==1993]=dl8$Pl_21[dl8$Ey==1993]#1993:21,24,22,25,23(by vote, not seats, as CSES)
dl8$Pl.2[dl8$Ey==1993]=dl8$Pl_24[dl8$Ey==1993]
dl8$Pl.3[dl8$Ey==1993]=dl8$Pl_22[dl8$Ey==1993]
dl8$Pl.4[dl8$Ey==1993]=dl8$Pl_25[dl8$Ey==1993]
dl8$Pl.5[dl8$Ey==1993]=dl8$Pl_23[dl8$Ey==1993]
dl8$Pl.1[dl8$Ey==1997]=dl8$Pl_21[dl8$Ey==1997]#1997:21,24,22,23,25(by vote, not seats, as CSES)
dl8$Pl.2[dl8$Ey==1997]=dl8$Pl_24[dl8$Ey==1997]
dl8$Pl.3[dl8$Ey==1997]=dl8$Pl_22[dl8$Ey==1997]
dl8$Pl.4[dl8$Ey==1997]=dl8$Pl_23[dl8$Ey==1997]
dl8$Pl.5[dl8$Ey==1997]=dl8$Pl_25[dl8$Ey==1997]
#already 0-10 scale
#leader likability:
dl8$Ll.1=NA
dl8$Ll.2=NA
dl8$Ll.3=NA
dl8$Ll.4=NA
dl8$Ll.5=NA
dl8$Ll.6=NA
# dl8$Ll.7=NA
dl8$Ll.1[dl8$Ey==1968]=dl8$Ll_21[dl8$Ey==1968]#1968:21,22,23
dl8$Ll.2[dl8$Ey==1968]=dl8$Ll_22[dl8$Ey==1968]
dl8$Ll.3[dl8$Ey==1968]=dl8$Ll_23[dl8$Ey==1968]
dl8$Ll.1[dl8$Ey==1974]=dl8$Ll_21[dl8$Ey==1974]#1974:21,22,23
dl8$Ll.2[dl8$Ey==1974]=dl8$Ll_22[dl8$Ey==1974]
dl8$Ll.3[dl8$Ey==1974]=dl8$Ll_23[dl8$Ey==1974]
dl8$Ll.1[dl8$Ey==1979]=dl8$Ll_21[dl8$Ey==1979]#1979:21,22,23(by vote, not seats, as CSES)
dl8$Ll.2[dl8$Ey==1979]=dl8$Ll_22[dl8$Ey==1979]
dl8$Ll.3[dl8$Ey==1979]=dl8$Ll_23[dl8$Ey==1979]
dl8$Ll.1[dl8$Ey==1988]=dl8$Ll_22[dl8$Ey==1988]#1988:22,21,23
dl8$Ll.2[dl8$Ey==1988]=dl8$Ll_21[dl8$Ey==1988]
dl8$Ll.3[dl8$Ey==1988]=dl8$Ll_23[dl8$Ey==1988]
dl8$Ll.1[dl8$Ey==1993]=dl8$Ll_21[dl8$Ey==1993]#1993:21,24,22,25,23(by vote, not seats, as CSES)
dl8$Ll.2[dl8$Ey==1993]=dl8$Ll_24[dl8$Ey==1993]
dl8$Ll.3[dl8$Ey==1993]=dl8$Ll_22[dl8$Ey==1993]
dl8$Ll.4[dl8$Ey==1993]=dl8$Ll_25[dl8$Ey==1993]
dl8$Ll.5[dl8$Ey==1993]=dl8$Ll_23[dl8$Ey==1993]
dl8$Ll.1[dl8$Ey==1997]=dl8$Ll_21[dl8$Ey==1997]#1997:21,24,22,23,25(by vote, not seats, as CSES)
dl8$Ll.2[dl8$Ey==1997]=dl8$Ll_24[dl8$Ey==1997]
dl8$Ll.3[dl8$Ey==1997]=dl8$Ll_22[dl8$Ey==1997]
dl8$Ll.4[dl8$Ey==1997]=dl8$Ll_23[dl8$Ey==1997]
dl8$Ll.5[dl8$Ey==1997]=dl8$Ll_25[dl8$Ey==1997]
#LRR=Left Right Respondent (voter LR self):
dl8$LRR=as.numeric(dl8$LRR)
#(no data)
#LRP.i=Left Right Parties:
dl8$LRP.1=NA#(no data)
dl8$LRP.2=NA#(no data)
dl8$LRP.3=NA#(no data)
dl8$LRP.4=NA#(no data)
dl8$LRP.5=NA#(no data)
dl8$LRP.6=NA#(no data)
# dl8$LRP.7=NA#(no data)
#Elections with LRR and LRP:
dl8$LR=NA#(no data)
#EeconS=Election economy state Subjective:(Retrospective socio-tropic economic evaluations)
dl8$EeconS=NA#(no data)
#Elections with EeconS:
dl8$Eecon=0#(no data)
#Seats to vote ratios:
dl8$P1P2rat=NA
dl8$P1P2rat[dl8$Ey==1968]=1.48
dl8$P1P2rat[dl8$Ey==1974]=1.22
dl8$P1P2rat[dl8$Ey==1979]=1.34#(by seats)
dl8$P1P2rat[dl8$Ey==1988]=1.51
dl8$P1P2rat[dl8$Ey==1993]=1.07#(by seats)
dl8$P1P2rat[dl8$Ey==1997]=1.30#(by seats)
#Gallagher Disproportionality measure:
dl8$GDM=NA
dl8$GDM[dl8$Ey==1968]=11.58
dl8$GDM[dl8$Ey==1974]=9.86
dl8$GDM[dl8$Ey==1979]=10.41
dl8$GDM[dl8$Ey==1988]=11.33
dl8$GDM[dl8$Ey==1993]=17.67
dl8$GDM[dl8$Ey==1997]=13.26
#Party family:
dl8$PartyFamilyCode21=40
dl8$PartyFamilyCode22=60
dl8$PartyFamilyCode23=30
dl8$PartyFamilyCode24=60
dl8$PartyFamilyCode25=90
#(single parties for vote alphabetical)
# # 21        LPC (Can) Liberal - Vn==1
# # 22         PC (Can) Progressive Conservative - Vn==2
# # 23        NDP (Can) New Democratic - Vn==3
# # 24         CA (Can) Reform - Vn==4
# # 25         BQ (Can) Bloc Québécois - Vn==5
dl8$Pf.1=NA
dl8$Pf.2=NA
dl8$Pf.3=NA
dl8$Pf.4=NA
dl8$Pf.5=NA
dl8$Pf.6=NA
# dl8$Pf.7=NA
dl8$Pf.1[dl8$Ey==1968]=dl8$PartyFamilyCode21[dl8$Ey==1968]#1968:21,22,23
dl8$Pf.2[dl8$Ey==1968]=dl8$PartyFamilyCode22[dl8$Ey==1968]
dl8$Pf.3[dl8$Ey==1968]=dl8$PartyFamilyCode23[dl8$Ey==1968]
dl8$Pf.1[dl8$Ey==1974]=dl8$PartyFamilyCode21[dl8$Ey==1974]#1974:21,22,23
dl8$Pf.2[dl8$Ey==1974]=dl8$PartyFamilyCode22[dl8$Ey==1974]
dl8$Pf.3[dl8$Ey==1974]=dl8$PartyFamilyCode23[dl8$Ey==1974]
dl8$Pf.1[dl8$Ey==1979]=dl8$PartyFamilyCode21[dl8$Ey==1979]#1979:21,22,23(by vote, not seats, as CSES)
dl8$Pf.2[dl8$Ey==1979]=dl8$PartyFamilyCode22[dl8$Ey==1979]
dl8$Pf.3[dl8$Ey==1979]=dl8$PartyFamilyCode23[dl8$Ey==1979]
dl8$Pf.1[dl8$Ey==1988]=dl8$PartyFamilyCode22[dl8$Ey==1988]#1988:22,21,23
dl8$Pf.2[dl8$Ey==1988]=dl8$PartyFamilyCode21[dl8$Ey==1988]
dl8$Pf.3[dl8$Ey==1988]=dl8$PartyFamilyCode23[dl8$Ey==1988]
dl8$Pf.1[dl8$Ey==1993]=dl8$PartyFamilyCode21[dl8$Ey==1993]#1993:21,24,22,25,23(by vote, not seats, as CSES)
dl8$Pf.2[dl8$Ey==1993]=dl8$PartyFamilyCode24[dl8$Ey==1993]
dl8$Pf.3[dl8$Ey==1993]=dl8$PartyFamilyCode22[dl8$Ey==1993]
dl8$Pf.4[dl8$Ey==1993]=dl8$PartyFamilyCode25[dl8$Ey==1993]
dl8$Pf.5[dl8$Ey==1993]=dl8$PartyFamilyCode23[dl8$Ey==1993]
dl8$Pf.1[dl8$Ey==1997]=dl8$PartyFamilyCode21[dl8$Ey==1997]#1997:21,24,22,23,25(by vote, not seats, as CSES)
dl8$Pf.2[dl8$Ey==1997]=dl8$PartyFamilyCode24[dl8$Ey==1997]
dl8$Pf.3[dl8$Ey==1997]=dl8$PartyFamilyCode22[dl8$Ey==1997]
dl8$Pf.4[dl8$Ey==1997]=dl8$PartyFamilyCode23[dl8$Ey==1997]
dl8$Pf.5[dl8$Ey==1997]=dl8$PartyFamilyCode25[dl8$Ey==1997]
#coding coalition status:
dl8$CoalCode=0
#CMPid.i:
dl8$CMPid.1=NA#create CMPid:
dl8$CMPid.2=NA
dl8$CMPid.3=NA
dl8$CMPid.4=NA
dl8$CMPid.5=NA
dl8$CMPid.6=NA
dl8$CMPid.1[dl8$Ey==1968]=62420#assign CMPid by party size order for parties with likability:
dl8$CMPid.2[dl8$Ey==1968]=62620
dl8$CMPid.3[dl8$Ey==1968]=62320
dl8$CMPid.1[dl8$Ey==1974]=62420
dl8$CMPid.2[dl8$Ey==1974]=62620
dl8$CMPid.3[dl8$Ey==1974]=62320
dl8$CMPid.1[dl8$Ey==1979]=62420
dl8$CMPid.2[dl8$Ey==1979]=62620
dl8$CMPid.3[dl8$Ey==1979]=62320
dl8$CMPid.1[dl8$Ey==1988]=62620
dl8$CMPid.2[dl8$Ey==1988]=62420
dl8$CMPid.3[dl8$Ey==1988]=62320
dl8$CMPid.1[dl8$Ey==1993]=62420
dl8$CMPid.2[dl8$Ey==1993]=62621
dl8$CMPid.3[dl8$Ey==1993]=62620
dl8$CMPid.4[dl8$Ey==1993]=62901
dl8$CMPid.5[dl8$Ey==1993]=62320
dl8$CMPid.1=as.numeric(dl8$CMPid.1)#recode as numeric:
dl8$CMPid.2=as.numeric(dl8$CMPid.2)
dl8$CMPid.3=as.numeric(dl8$CMPid.3)
dl8$CMPid.4=as.numeric(dl8$CMPid.4)
dl8$CMPid.5=as.numeric(dl8$CMPid.5)
dl8$CMPid.6=as.numeric(dl8$CMPid.6)
#PrePost:
dl8$PrePost=as.numeric(NA)
dl8$PrePost[dl8$Ey==1968]=1
dl8$PrePost[dl8$Ey==1974]=1
dl8$PrePost[dl8$Ey==1979]=1
dl8$PrePost[dl8$Ey==1988]=3
dl8$PrePost[dl8$Ey==1993]=3
#"Pi"#Party identification:
dl8$Pitemp=dl8$Pi#set Pi to Pitemp for ordering Pi by seat share
dl8$Pi=NA
dl8$Pi[dl8$Ey==1968&dl8$Pitemp==1]=1#1968:21,22,23
dl8$Pi[dl8$Ey==1968&dl8$Pitemp==2]=2
dl8$Pi[dl8$Ey==1968&dl8$Pitemp==3]=3
dl8$Pi[dl8$Ey==1974&dl8$Pitemp==1]=1#1974:21,22,23
dl8$Pi[dl8$Ey==1974&dl8$Pitemp==2]=2
dl8$Pi[dl8$Ey==1974&dl8$Pitemp==3]=3
dl8$Pi[dl8$Ey==1979&dl8$Pitemp==1]=1#1979:21,22,23(by vote, not seats, as CSES)
dl8$Pi[dl8$Ey==1979&dl8$Pitemp==2]=2
dl8$Pi[dl8$Ey==1979&dl8$Pitemp==3]=3
dl8$Pi[dl8$Ey==1988&dl8$Pitemp==2]=1#1988:22,21,23
dl8$Pi[dl8$Ey==1988&dl8$Pitemp==1]=2
dl8$Pi[dl8$Ey==1988&dl8$Pitemp==3]=3
dl8$Pi[dl8$Ey==1993&dl8$Pitemp==1]=1#1993:21,24,22,25,23(by vote, not seats, as CSES)
dl8$Pi[dl8$Ey==1993&dl8$Pitemp==4]=2
dl8$Pi[dl8$Ey==1993&dl8$Pitemp==2]=3
dl8$Pi[dl8$Ey==1993&dl8$Pitemp==5]=4
dl8$Pi[dl8$Ey==1993&dl8$Pitemp==3]=5
dl8$Pi[dl8$Ey==1997&dl8$Pitemp==1]=1#1997:21,24,22,23,25(by vote, not seats, as CSES)
dl8$Pi[dl8$Ey==1997&dl8$Pitemp==4]=2
dl8$Pi[dl8$Ey==1997&dl8$Pitemp==2]=3
dl8$Pi[dl8$Ey==1997&dl8$Pitemp==3]=4
dl8$Pi[dl8$Ey==1997&dl8$Pitemp==5]=5
dl8$Pi=as.numeric(dl8$Pi)
#subset:
colnames(dl8)
dl8=dl8[,c(1,2,3,31,4,30,32:43,50,6,44:49,52,51,53,54,60:73,29,8)]
dl8=as.data.frame(dl8)
colnames(dl8)
# dl8[colnames(dl8)]=sapply(dl8[colnames(dl8)],as.numeric)


#DL SPAIN (dl9):----
#all elections are already coded in tev, therefore not coded.


#MERGE EV AND LP:----

#merge European Voter dataset, and the Leadership Project Dataset in de:
de=rbind(de1,de2,de3,de4,de5,de6,dl7,dl8)
colnames(de)
rm(de1,de2,de3,de4,de5,de6,dl7,dl8,dl)

#creating variable missing in one of the datasets compared to the other:
de$Ec[de$Ec==1]="GBR"
de$Ec[de$Ec==2]="DNK"
de$Ec[de$Ec==3]="DEU"
de$Ec[de$Ec==4]="NLD"
de$Ec[de$Ec==5]="NOR"
de$Ec[de$Ec==6]="SWE"
de$Ec[de$Ec==7]="AUS"
de$Ec[de$Ec==8]="CAN"
de$Es=paste0(de$Ec,"_",de$Ey)#election study


#creating party family voted party:----
x=as.data.frame(de)
X=x[, names(x) %in% paste0("Pf.", 1:6)]
X=as.matrix(X)
de$VPf=NA
for (i in 1:nrow(de)) {
  de$VPf[i]=X[i,de$Va[i]]
}
rm(x,X)


#Pv.i (pervote):----

#read CMP dataset:
dCMP=read.xlsx("MPDataset_MPDS2020b.xlsx")

#filter CMP dataset and set variable names:
colnames(dCMP)[colnames(dCMP)=="party"]="CMPid_temp"
colnames(dCMP)[colnames(dCMP)=="pervote"]="Pv"

dCMP$Ec=NA
dCMP$Ec=as.character(dCMP$Ec)
dCMP$Ec[dCMP$country==63]="AUS"
dCMP$Ec[dCMP$country==42]="AUT"
dCMP$Ec[dCMP$country==62]="CAN"
dCMP$Ec[dCMP$country==41]="DEU"
dCMP$Ec[dCMP$country==13]="DNK"
dCMP$Ec[dCMP$country==33]="ESP"
dCMP$Ec[dCMP$country==14]="FIN"
dCMP$Ec[dCMP$country==51]="GBR"
dCMP$Ec[dCMP$country==34]="GRC"
dCMP$Ec[dCMP$country==53]="IRL"
dCMP$Ec[dCMP$country==15]="ISL"
dCMP$Ec[dCMP$country==72]="ISR"
dCMP$Ec[dCMP$country==32]="ITA"
dCMP$Ec[dCMP$country==22]="NLD"
dCMP$Ec[dCMP$country==12]="NOR"
dCMP$Ec[dCMP$country==64]="NZL"
dCMP$Ec[dCMP$country==35]="PRT"
dCMP$Ec[dCMP$country==11]="SWE"
dCMP$Ec[dCMP$country==83]="EST"#
dCMP$Ec[dCMP$country==86]="HUN"
dCMP$Ec[dCMP$country==88]="LTU"
dCMP$Ec[dCMP$country==97]="SVN"

dCMP$Ey=as.numeric(substr(dCMP$date,1,4))

dCMP$Ey[dCMP$Ec=="GBR"&dCMP$date==197410]=1975#set temporary Ey for double elections

#keep only variables of interest:
dCMP=dCMP[,c(175,176,7,16)]

#code pervote alternative i (Pv.i):
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.1"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T, all.y=F)
colnames(de)[colnames(de)=="Pv"]="Pv.1"
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.2"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="Pv"]="Pv.2"
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.3"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="Pv"]="Pv.3"
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.4"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="Pv"]="Pv.4"
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.5"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="Pv"]="Pv.5"
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.6"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="Pv"]="Pv.6"


#Ps.i (perseats):----

#read CMP dataset:
dCMP=read.xlsx("MPDataset_MPDS2020b.xlsx")

#filter CMP dataset and set variable names:
colnames(dCMP)[colnames(dCMP)=="party"]="CMPid_temp"
colnames(dCMP)[colnames(dCMP)=="absseat"]="Ps"
colnames(dCMP)[colnames(dCMP)=="totseats"]="Pst"

dCMP$Ec=NA
dCMP$Ec=as.character(dCMP$Ec)
dCMP$Ec[dCMP$country==63]="AUS"
dCMP$Ec[dCMP$country==42]="AUT"
dCMP$Ec[dCMP$country==62]="CAN"
dCMP$Ec[dCMP$country==41]="DEU"
dCMP$Ec[dCMP$country==13]="DNK"
dCMP$Ec[dCMP$country==33]="ESP"
dCMP$Ec[dCMP$country==14]="FIN"
dCMP$Ec[dCMP$country==51]="GBR"
dCMP$Ec[dCMP$country==34]="GRC"
dCMP$Ec[dCMP$country==53]="IRL"
dCMP$Ec[dCMP$country==15]="ISL"
dCMP$Ec[dCMP$country==72]="ISR"
dCMP$Ec[dCMP$country==32]="ITA"
dCMP$Ec[dCMP$country==22]="NLD"
dCMP$Ec[dCMP$country==12]="NOR"
dCMP$Ec[dCMP$country==64]="NZL"
dCMP$Ec[dCMP$country==35]="PRT"
dCMP$Ec[dCMP$country==11]="SWE"
dCMP$Ec[dCMP$country==83]="EST"#
dCMP$Ec[dCMP$country==86]="HUN"
dCMP$Ec[dCMP$country==88]="LTU"
dCMP$Ec[dCMP$country==97]="SVN"

dCMP$Ey=as.numeric(substr(dCMP$date,1,4))

dCMP$Ey[dCMP$Ec=="GBR"&dCMP$date==197410]=1975#set temporary Ey for double elections

#code total seats:
dCMP_temp=dCMP[,c(175,176,7,20)]
de=merge(de, dCMP_temp, by.x=c("Ec","Ey","CMPid.1"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T, all.y=F)
rm(dCMP_temp)

#keep only variables of interest:
dCMP=dCMP[,c(175,176,7,19)]

#code seats alternative i (Ps.i):
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.1"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="Ps"]="Ps.1"
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.2"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="Ps"]="Ps.2"
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.3"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="Ps"]="Ps.3"
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.4"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="Ps"]="Ps.4"
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.5"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="Ps"]="Ps.5"
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.6"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="Ps"]="Ps.6"

#get seat share %:
de$Ps.1=de$Ps.1/de$Pst*100
de$Ps.2=de$Ps.2/de$Pst*100
de$Ps.3=de$Ps.3/de$Pst*100
de$Ps.4=de$Ps.4/de$Pst*100
de$Ps.5=de$Ps.5/de$Pst*100
de$Ps.6=de$Ps.6/de$Pst*100


#Seats to vote ratios:----
de$P1SVrat=de$Ps.1/de$Pv.1#for most voted party
de$P2SVrat=de$Ps.2/de$Pv.2#for second most voted party
de$P1P2rat=de$P1SVrat/de$P2SVrat#ratio of rations first/second party
summary(de$P1P2rat)


#Gallagher Disproportionality measure:----
#(coded only for the elections that will remain in the final dataset)
de$GDM=NA
de$GDM[de$Es=="AUS_1993"]=8.12
de$GDM[de$Es=="CAN_1968"]=11.58
de$GDM[de$Es=="CAN_1974"]=9.86
de$GDM[de$Es=="CAN_1979"]=10.41
de$GDM[de$Es=="CAN_1988"]=11.33
de$GDM[de$Es=="CAN_1993"]=17.67
de$GDM[de$Es=="DNK_1971"]=3.45
de$GDM[de$Es=="DNK_1973"]=1.20
de$GDM[de$Es=="DNK_1994"]=1.57
de$GDM[de$Es=="GBR_1974"]=15.47#1974f
de$GDM[de$Es=="GBR_1975"]=14.96#1974o
de$GDM[de$Es=="GBR_1979"]=11.58
de$GDM[de$Es=="NLD_1986"]=1.67


#CMPrile:----

#read CMP dataset:
dCMP=read.xlsx("MPDataset_MPDS2020b.xlsx")

#filter CMP dataset and set variable names:
colnames(dCMP)[colnames(dCMP)=="party"]="CMPid_temp"
colnames(dCMP)[colnames(dCMP)=="rile"]="CMPrile"

dCMP$Ec=NA
dCMP$Ec=as.character(dCMP$Ec)
dCMP$Ec[dCMP$country==63]="AUS"
dCMP$Ec[dCMP$country==42]="AUT"
dCMP$Ec[dCMP$country==62]="CAN"
dCMP$Ec[dCMP$country==41]="DEU"
dCMP$Ec[dCMP$country==13]="DNK"
dCMP$Ec[dCMP$country==33]="ESP"
dCMP$Ec[dCMP$country==14]="FIN"
dCMP$Ec[dCMP$country==51]="GBR"
dCMP$Ec[dCMP$country==34]="GRC"
dCMP$Ec[dCMP$country==53]="IRL"
dCMP$Ec[dCMP$country==15]="ISL"
dCMP$Ec[dCMP$country==72]="ISR"
dCMP$Ec[dCMP$country==32]="ITA"
dCMP$Ec[dCMP$country==22]="NLD"
dCMP$Ec[dCMP$country==12]="NOR"
dCMP$Ec[dCMP$country==64]="NZL"
dCMP$Ec[dCMP$country==35]="PRT"
dCMP$Ec[dCMP$country==11]="SWE"
dCMP$Ec[dCMP$country==83]="EST"#
dCMP$Ec[dCMP$country==86]="HUN"
dCMP$Ec[dCMP$country==88]="LTU"
dCMP$Ec[dCMP$country==97]="SVN"

dCMP$Ey=as.numeric(substr(dCMP$date,1,4))

dCMP$Ey[dCMP$Ec=="GBR"&dCMP$date==197410]=1975#set temporary Ey for double elections

#keep only variables of interest:
dCMP=dCMP[,c(175,176,7,168)]

#code pervote alternative i (CMPrile.i):
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.1"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="CMPrile"]="CMPrile.1"
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.2"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="CMPrile"]="CMPrile.2"
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.3"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="CMPrile"]="CMPrile.3"
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.4"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="CMPrile"]="CMPrile.4"
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.5"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="CMPrile"]="CMPrile.5"
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.6"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="CMPrile"]="CMPrile.6"


#CMPper415:----

#read CMP dataset:
dCMP=read.xlsx("MPDataset_MPDS2020b.xlsx")

#filter CMP dataset and set variable names:
colnames(dCMP)[colnames(dCMP)=="party"]="CMPid_temp"
colnames(dCMP)[colnames(dCMP)=="per415"]="CMPper415"

dCMP$Ec=NA
dCMP$Ec=as.character(dCMP$Ec)
dCMP$Ec[dCMP$country==63]="AUS"
dCMP$Ec[dCMP$country==42]="AUT"
dCMP$Ec[dCMP$country==62]="CAN"
dCMP$Ec[dCMP$country==41]="DEU"
dCMP$Ec[dCMP$country==13]="DNK"
dCMP$Ec[dCMP$country==33]="ESP"
dCMP$Ec[dCMP$country==14]="FIN"
dCMP$Ec[dCMP$country==51]="GBR"
dCMP$Ec[dCMP$country==34]="GRC"
dCMP$Ec[dCMP$country==53]="IRL"
dCMP$Ec[dCMP$country==15]="ISL"
dCMP$Ec[dCMP$country==72]="ISR"
dCMP$Ec[dCMP$country==32]="ITA"
dCMP$Ec[dCMP$country==22]="NLD"
dCMP$Ec[dCMP$country==12]="NOR"
dCMP$Ec[dCMP$country==64]="NZL"
dCMP$Ec[dCMP$country==35]="PRT"
dCMP$Ec[dCMP$country==11]="SWE"
dCMP$Ec[dCMP$country==83]="EST"#
dCMP$Ec[dCMP$country==86]="HUN"
dCMP$Ec[dCMP$country==88]="LTU"
dCMP$Ec[dCMP$country==97]="SVN"

dCMP$Ey=as.numeric(substr(dCMP$date,1,4))

dCMP$Ey[dCMP$Ec=="GBR"&dCMP$date==197410]=1975#set temporary Ey for double elections

#keep only variables of interest:
dCMP=dCMP[,c(175,176,7,59)]

#code pervote alternative i (CMPper415.i):
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.1"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="CMPper415"]="CMPper415.1"
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.2"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="CMPper415"]="CMPper415.2"
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.3"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="CMPper415"]="CMPper415.3"
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.4"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="CMPper415"]="CMPper415.4"
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.5"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="CMPper415"]="CMPper415.5"
de=merge(de, dCMP, by.x=c("Ec","Ey","CMPid.6"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(de)[colnames(de)=="CMPper415"]="CMPper415.6"


#Ed election dataset:----
de$Ed="EV-LP"


#Rid Respondent ID:----
de$Rid=NA#there is not unique ID


#SET DOUBLE ELECTIONS BACK TO ORIGINAL VALUES:----

#settign GBR 1974 elections back to correct year and election study:
de$Es[de$Ec=="GBR"&de$Ey==1974]="GBR_1974f"#February election
de$Es[de$Ec=="GBR"&de$Ey==1975]="GBR_1974o"#October election
de$Ey[de$Ec=="GBR"&de$Ey==1975]=1974


#FILTER AND MERGE EV-LP AND CSES:----

#keep in EV-LP only elections not already present in CSES:
sort(unique(dc$Es[dc$Es %in% c(sort(unique(de$Es)))]))
#"AUS_1996" "CAN_1997" "DEU_1998" "DNK_1998" "GBR_1997" "NLD_1998" "NOR_1997" "SWE_1998"
de=de[!(de$Es %in% c(sort(unique(dc$Es)))),]

#filter European Voter and CSES data:
names(dc)
names(de)
dc=dc[,-c(12,55:60)]
de=de[,c(1:2,47,76,77,9,45,10,43,11,12,13:24,26,27:32,34:36,48,44,49:54,56:61,64:69,8:3,70:75,46)]


#merge European Voter and CSES data in de:
de=rbind(de,dc)
rm(dc)
de=as.data.frame(de)


#SAVE DATA:----
dew=de#remane to match TEV data creation code
rm(de)
save(dew, file = "d29_CSES-EV-LP.RData")

